Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on any exit code if configured to fail #18

Merged
merged 15 commits into from
Apr 25, 2024
Merged
2 changes: 1 addition & 1 deletion .github/workflows/binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- id: generate
run: |
cat >>"$GITHUB_OUTPUT" <<EOM
Expand Down
36 changes: 26 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,42 @@ on:
branches: main

jobs:
test:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: freckle/stack-cache-action@v2
with:
working-directory: example
strategy:
matrix:
working-directory:
- example
- example-9.6

steps:
- uses: actions/checkout@v4

- uses: freckle/stack-action@v3
- id: stack
uses: freckle/stack-action@v5
with:
working-directory: example
working-directory: ${{ matrix.working-directory }}

- id: weeder
uses: ./
with:
ghc-version: 9.2.5
working-directory: example
ghc-version: ${{ steps.stack.outputs.compiler-version }}
working-directory: ${{ matrix.working-directory }}
fail: false

- run: |
# Expected to find this unused function
grep -F goodbyeWorld "${{ steps.weeder.outputs.log }}"

# This Job will only run (and fail) if the tests matrix fails. This way, we
# can make it our required status (skipped == green) rather than having to
# maintain a required status per element of the tests matrix.
test:
needs: tests
if: ${{ failure() || cancelled() }}
runs-on: ubuntu-latest
steps:
- run: |
echo "Upstream Jobs have failed or been cancelled"
exit 1
46 changes: 42 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ runs:
version: ${{ inputs.ghc-version }}
url: "https://github.com/freckle/weeder-action/releases/download/Binaries/{name}-{version}-{os}-{arch}.{ext}"

- id: weeder-version
name: Record weeder version
shell: bash
run: |
{
printf 'result='
weeder --version | sed '/^weeder version \(.*\)$/!d; s//\1/'
} >>"$GITHUB_OUTPUT"
pbrisbin marked this conversation as resolved.
Show resolved Hide resolved

- id: weeder-check
name: Compare weeder version with v2.7
uses: pbrisbin/semver-compare-action@master
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this fork just removes warnings about an outdated version of Node?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does. The PR sits open with no response for weeks now though, so I might be just taking over for myself...

with:
left-semver: ${{ steps.weeder-version.outputs.result }}
right-semver: 2.7.0
pbrisbin marked this conversation as resolved.
Show resolved Hide resolved

- id: weeder
name: Run weeder
shell: bash
env:
WEEDER_VERCMP: ${{ steps.weeder-check.outputs.compare-value }}
run: |
echo "::add-matcher::${{ github.action_path }}/matcher.json"
trap 'echo "::remove-matcher owner=freckle-weeder::"' EXIT
Expand All @@ -43,11 +61,31 @@ runs:

cd '${{ inputs.working-directory }}'
prefix=$(echo '${{ inputs.working-directory }}' | sed 's|/$||')/
weeder ${{ inputs.weeder-arguments }} |

ret=0

if ! weeder ${{ inputs.weeder-arguments }} |
sed "s|^|$prefix|; "'s/$/ is not used by any defined root/' |
tee "$tmp" || true
tee "$tmp"; then
ret=${PIPESTATUS[0]} # can't trust $? here, apparently
fi

echo "Weeder exit code: $ret"

if ((!ret)); then
echo "No unused functions found"
exit 0
fi

if ${{ inputs.fail }} && [[ -s "$tmp" ]]; then
# If we're configured to fail, we can always fail regardless of version
if ${{ inputs.fail }}; then
echo "Configured to fail and unused functions found"
exit 1
exit $ret
fi

# Otherwise, if we know we're v2.7+ and the exit code was not
# weeds-found (228), we should still fail.
if ((WEEDER_VERCMP >= 0)) && ((ret != 228)); then
echo "Weeder encountered some other error" >&2
exit $ret
fi
8 changes: 8 additions & 0 deletions example-9.6/app/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Main where

import Prelude

import Lib

main :: IO ()
main = putStrLn helloWorld
31 changes: 31 additions & 0 deletions example-9.6/example.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack

name: example
version: 0.0.0.0
build-type: Simple

library
exposed-modules:
Lib
other-modules:
Paths_example
hs-source-dirs:
src
build-depends:
base
default-language: Haskell2010

executable example
main-is: Main.hs
other-modules:
Paths_example
hs-source-dirs:
app
build-depends:
base
, example
default-language: Haskell2010
15 changes: 15 additions & 0 deletions example-9.6/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: example
version: 0.0.0.0

dependencies:
- base

library:
source-dirs: src

executables:
example:
source-dirs: app
main: Main.hs
dependencies:
- example
16 changes: 16 additions & 0 deletions example-9.6/src/Lib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Lib
( helloWorld
, goodbyeWorld
, thisFunction
) where

import Prelude

helloWorld :: String
helloWorld = "Hello world"

goodbyeWorld :: String
goodbyeWorld = "Goodbye world"

Check failure on line 13 in example-9.6/src/Lib.hs

View workflow job for this annotation

GitHub Actions / tests (example-9.6)

goodbyeWorld is not used by any defined root

thisFunction :: String
thisFunction = "Is unused"

Check failure on line 16 in example-9.6/src/Lib.hs

View workflow job for this annotation

GitHub Actions / tests (example-9.6)

thisFunction is not used by any defined root
3 changes: 3 additions & 0 deletions example-9.6/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resolver: lts-22.15
ghc-options:
"$locals": -fwrite-ide-info
12 changes: 12 additions & 0 deletions example-9.6/stack.yaml.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files

packages: []
snapshots:
- completed:
sha256: 5b002d57c51092aa58a8696ccf0993e74fa6ed2efd48e2bbca349e9c2f67c5ef
size: 713334
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/22/15.yaml
original: lts-22.15
2 changes: 2 additions & 0 deletions example-9.6/weeder.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
roots = [ "^Main\\.main$", "^Paths_.*" ]
type-class-roots = true
Loading