Skip to content

Automating#

Pre-commit#

Note

Only commit clean code.

pip install pre-commit
  • add .pre-commit-config.yaml to your repository
  • add basic "hooks"
  • add hooks for miss_hit
  • run on all files
  • commit
---
exclude: "mkdocs.yml"
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v5.0.0
  hooks:
  - id: trailing-whitespace
  - id: end-of-file-fixer
  - id: check-yaml
  - id: check-json
  - id: check-added-large-files
  - id: check-case-conflict
  - id: check-merge-conflict

- repo: local
  hooks:
  - id: mh_style
    name: mh_style
    entry: mh_style
    args: [--process-slx, --fix]
    files: ^(.*\.(m|slx))$
    language: python
    additional_dependencies: [miss_hit_core]

  - id: mh_metric
    name: mh_metric
    entry: mh_metric
    args: [--ci]
    files: ^(.*\.(m|slx))$
    language: python
    additional_dependencies: [miss_hit_core]

  - id: mh_lint
    name: mh_lint
    entry: mh_lint
    files: ^(.*\.(m|slx))$
    language: python
    additional_dependencies: [miss_hit]

CI/CD in github#

  • create a github repository
  • push your code to github
  • create a workflow to
    • checkout your repository
    • install miss_hit
    • run miss_hit on your code
---
name: style

# Controls when the action will run.
# Triggers the workflow on push or pull request
on:
  push:
    branches: ['*']
  pull_request:
    branches: ['*']

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Cancel previous runs that are not completed yet
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    # Defines operating system to run on
    runs-on: ubuntu-latest

    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE,
    # so your job can access it
    - name: Clone repo
      # documentation: https://github.com/actions/checkout
      uses: actions/checkout@v4

    # We install the dependecies
    # python via a github action
    # miss_hit with pip
    - name: install python
      uses: actions/setup-python@v5
      with:
        python-version: '3.10'
    - name: install miss hit
      run: pip install miss_hit

    # run miss_hit
    - name: lint
      run: mh_lint .
    - name: style
      run: mh_style .
    - name: metric
      run: mh_metric --ci .

Pre-commit-CI#

Warning

Only works on github repositories.

Running tests in github#

  • create a workflow to
    • checkout your repository
    • install MoxUnit and MoCov
    • install Matlab
    • run your tests

This workflow is in .github/workflows/tests.yml.

---
name: tests

# Controls when the action will run.
# Triggers the workflow on push or pull request
on:
  push:
    branches: ['*']
  pull_request:
    branches: ['*']

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Cancel previous runs that are not completed yet
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    # Defines operating system to run on
    runs-on: ubuntu-latest

    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE,
    # so your job can access it
    - name: Clone repo
      # documentation: https://github.com/actions/checkout
      uses: actions/checkout@v4

    # We install the dependecies
    # MoxUnit and MOcov for testing and coverage
    # MATLAB via a github action for running the tests
    - name: Install Moxunit and MOcov
      run: |
        git clone https://github.com/MOxUnit/MOxUnit.git --depth 1
        git clone https://github.com/MOcov/MOcov.git --depth 1
    - name: Install MATLAB
      # documentation: https://github.com/matlab-actions/setup-matlab
      uses: matlab-actions/setup-matlab@v2.3.0
      with:
        release: R2023b

    # Actually run the tests by running the script:
    #  .github/workflows/Tests_Matlab.m
    - name: Run unit tests MATLAB
      # documentation: https://github.com/matlab-actions/run-command
      uses: matlab-actions/run-command@v2.1.1
      with:
        command: |
          cd(fullfile(getenv('GITHUB_WORKSPACE'), '.github', 'workflows')); run Tests_Matlab;

    # When the tests are run they generate a log file
    # Here we check the content to see if there were any errors
    - name: Check unit tests
      run: cat doc/testing/test_report.log | grep 0

It calls the MATLAB script .github/workflows/Tests_Matlab.m.

% (C) Copyright 2023 Remi Gau developers
%
% script to run the tests with MOxUnit
%
% This script can be run locally or on github CI.
%

root_dir = getenv('GITHUB_WORKSPACE');
% set up for testing in github CI
if ~isempty(root_dir)
    addpath(fullfile(root_dir, 'MOcov', 'MOcov'));

    cd(fullfile(root_dir, 'MOxUnit', 'MOxUnit'));
    run moxunit_set_path();

    % set up for local testing
else
    root_dir = fullfile(fileparts(mfilename('fullpath')), '..', '..');

end

% run tests for the example function created during the workshop
cd(fullfile(root_dir, 'doc', 'testing'));
run Run_Tests;

% run tests on the legacy code
cd(root_dir);
run Run_Tests_Legacy_Code;

CI/CD in gitlab#

Setting up a CI/CD pipeline#

---
.matlab_defaults:
  image:
    # Replace the value with the name
    # of the MATLAB container image you want to use
    name: mathworks/matlab:latest
    entrypoint: ['']
  variables:
    # Replace the value with the port number
    # and DNS address for your network license manager
    MLM_LICENSE_FILE: 27000@licmatlab.hrz.uni-marburg.de

stages:
- lint
- build
- test

miss_hit:
  stage: lint
  # use a python docker image
  image: python:latest
  script:
  # install miss_hit
  - pip install miss_hit
  # run miss_hit
  - mh_lint
  - mh_style
  - mh_metric --ci

set-up:
  stage: build

  # FIXME
  #
  # cache:
  #   key: $CI_COMMIT_REF_SLUG
  #   paths:
  #   - MOxUnit
  #   - MOcov

  script:
  # install git and get MOcov and MOxUnit
  - apk update && apk add git
  - git clone https://github.com/MOxUnit/MOxUnit.git --depth 1
  - git clone https://github.com/MOcov/MOcov.git --depth 1

# TODO
#
# test:
#   stage: test
#   cache:
#     key: $CI_COMMIT_REF_SLUG
#     paths:
#     - MOxUnit
#     - MOcov
#   extends: .matlab_defaults
#   script:
#   - matlab -batch mycommand