Skip to content

Commit

Permalink
Add code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years committed Oct 18, 2023
1 parent c2e593b commit 3096496
Show file tree
Hide file tree
Showing 13 changed files with 342 additions and 26 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
on:
pull_request:

name: Code coverage

permissions:
issues: write
pull-requests: write

jobs:
coverage-report:
name: Generate test coverage report
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- uses: cachix/install-nix-action@v22
with:
extra_nix_config: |
extra-experimental-features = nix-command flakes
accept-flake-config = true
- name: Fetch code coverage report
id: fetch-coverage
run: |
COVERAGE_PATH=$(nix build '.#coverage' --print-build-logs --print-out-paths --no-link)
echo "coverage-path=$COVERAGE_PATH" >> "$GITHUB_OUTPUT"
- name: Read coverage summary
id: coverage-summary
uses: juliangruber/read-file-action@v1
with:
path: ${{ steps.fetch-coverage.outputs.coverage-path }}/coverage-summary.txt

- name: Comment with coverage information
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
Code coverage:
```
${{ steps.coverage-summary.outputs.content }}
```
- uses: actions/upload-artifact@v3
with:
name: coverage-report
path: ${{ steps.fetch-coverage.outputs.coverage-path }}/coverage-report
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
9 changes: 9 additions & 0 deletions .luacov
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
return {
modules = {
["broot"] = "lua/broot/init.lua",
["broot.*"] = "lua",
},
statsfile = "target/coverage.stats",
reportfile = "target/coverage.lcov",
tick = true,
}
39 changes: 35 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,44 @@ ifdef file
NVIM_COMMAND = "lua MiniTest.run_file([["$(file)"]])"
endif

# Run all test files
.PHONY: test
test:
nvim --headless --noplugin -u ./scripts/mini_test_init.lua -c $(NVIM_COMMAND)
ifdef coverage
export COVERAGE = 1
endif

# Clean out temporary files or other artifacts.
.PHONY: clean
clean:
rm -rf target

# Format code
.PHONY: format
format:
stylua .
alejandra .

# Run all test files, or the file specified by `file=`.
.PHONY: test
test: target
nvim --headless --noplugin -u ./scripts/mini_test_init.lua -c $(NVIM_COMMAND)
ifdef COVERAGE
make target/coverage-report
endif

# Generate an HTML coverage report and fail if there are untested lines.
target/coverage-report target/coverage-summary.txt: target target/coverage.lcov
genhtml target/coverage.lcov \
--no-function-coverage \
--no-branch-coverage \
--output-directory target/coverage-report
lcov --summary target/coverage.lcov \
--fail-under-lines 100 \
| tee target/coverage-summary.txt
lcov --list target/coverage.lcov \
| tee --append target/coverage-summary.txt

target/coverage.lcov: target/coverage.stats
luacov -r lcov

# Create the `target` directory.
target:
mkdir -p target
51 changes: 51 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 62 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,44 @@

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";

# Neovim test framework.
mini-nvim = {
url = "github:echasnovski/mini.nvim";
flake = false;
};

# Vimhelp->HTML generation. Used for the GitHub pages site.
vimhelp = {
url = "github:c4rlo/vimhelp";
flake = false;
};

# Tool for running `lua-language-server` in check mode for type-checking.
lualscheck = {
url = "github:9999years/lualscheck";
flake = false;
};

# Neovim Lua API type stubs.
neodev = {
url = "github:folke/neodev.nvim";
flake = false;
};

# Lua code coverage.
luacov = {
url = "github:lunarmodules/luacov";
flake = false;
};
cluacov = {
url = "github:mpeterv/cluacov";
flake = false;
};
luacov-reporter-lcov = {
url = "github:daurnimator/luacov-reporter-lcov";
flake = false;
};
};

nixConfig = {
Expand All @@ -34,6 +56,9 @@
vimhelp,
lualscheck,
neodev,
luacov,
cluacov,
luacov-reporter-lcov,
}: let
forAllSystems = function:
nixpkgs.lib.genAttrs [
Expand All @@ -54,6 +79,7 @@
mkCheck = pkgs.callPackage ./nix/mkCheck.nix {
luarc = self.packages.${pkgs.system}.luarc;
};
luaPkgs = pkgs.lua5_1.pkgs;
in {
# mini.nvim unit tests.
tests = mkCheck {
Expand All @@ -73,6 +99,27 @@
'';
};

luacov = self.checks.${pkgs.system}.tests.override (old: {
name = "luacov";

nativeCheckInputs =
(old.nativeCheckInputs or [])
++ [
self.packages.${pkgs.system}.luacov
self.packages.${pkgs.system}.cluacov
pkgs.lcov
];

COVERAGE = true;

installPhase = ''
mkdir $out
cp target/coverage.lcov $out/
cp target/coverage-summary.txt $out/
cp --recursive target/coverage-report $out/
'';
});

# Check diagnostics and type annotations with `lua-language-server`.
luals = mkCheck {
name = "lua-language-server";
Expand Down Expand Up @@ -116,7 +163,7 @@
luacheck = mkCheck {
name = "luacheck";

nativeCheckInputs = [pkgs.lua5_1.pkgs.luacheck];
nativeCheckInputs = [luaPkgs.luacheck];

checkPhase = ''
luacheck .
Expand All @@ -135,16 +182,27 @@
};

luarc = pkgs.callPackage ./nix/luarc.nix {inherit neodev;};

luacov = pkgs.callPackage ./nix/luacov.nix {
inherit luacov;
luacov-reporter-lcov = self.packages.${pkgs.system}.luacov-reporter-lcov;
};
cluacov = pkgs.callPackage ./nix/cluacov.nix {
inherit cluacov;
luacov = self.packages.${pkgs.system}.luacov;
};
luacov-reporter-lcov = pkgs.callPackage ./nix/luacov-reporter-lcov.nix {
inherit luacov-reporter-lcov;
};

coverage = self.checks.${pkgs.system}.luacov;
});

devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
MINI_NVIM = "${mini-nvim}";

inputsFrom = builtins.attrValues self.checks.${pkgs.system};
packages = [
pkgs.lua5_1.pkgs.luacheck
];

shellHook = ''
${self.packages.${pkgs.system}.luarc.link-to-cwd}
Expand Down
15 changes: 15 additions & 0 deletions nix/cluacov.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
lua5_1,
luacov,
cluacov,
}:
lua5_1.pkgs.buildLuarocksPackage {
pname = "cluacov";
version = "scm-1";
src = cluacov;

propagatedBuildInputs = [
lua5_1
luacov
];
}
17 changes: 17 additions & 0 deletions nix/luacov-reporter-lcov.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
lua5_1,
luacov-reporter-lcov,
}:
lua5_1.pkgs.buildLuarocksPackage {
pname = "luacov-reporter-lcov";
version = "scm-0";
src = luacov-reporter-lcov;

patches = [
./patches/luacov-reporter-lcov.diff
];

propagatedBuildInputs = [
lua5_1
];
}
25 changes: 25 additions & 0 deletions nix/luacov.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
lua5_1,
luacov,
luacov-reporter-lcov,
}:
lua5_1.pkgs.buildLuarocksPackage {
pname = "luacov";
version = "scm-1";
src = luacov;

propagatedBuildInputs = [
lua5_1
luacov-reporter-lcov
];

patches = [
./patches/luacov.diff
];

# Tell `luacov` where to find its assets.
preConfigure = ''
substituteInPlace src/luacov/reporter/html.lua \
--subst-var-by assetDir "$out/$rocksSubdir/$pname/$version"
'';
}
Loading

0 comments on commit 3096496

Please sign in to comment.