From 0e92cfa49197d6ccf43a93b1828d6172b2db1c97 Mon Sep 17 00:00:00 2001 From: rhysd Date: Thu, 24 Oct 2024 13:57:54 +0900 Subject: [PATCH] add support for Windows to update-checks-doc script --- .gitattributes | 1 + .github/workflows/ci.yaml | 2 -- Makefile | 3 ++- docs/checks.md | 1 + scripts/update-checks-doc/README.md | 3 --- scripts/update-checks-doc/main.go | 16 +++++++++++-- scripts/update-checks-doc/main_test.go | 32 ++++++++++++-------------- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/.gitattributes b/.gitattributes index 22e33ed62..b25b2fb38 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,3 +3,4 @@ /scripts/generate-webhook-events/testdata/** -text /scripts/generate-availability/testdata/** -text /scripts/generate-actionlint-matcher/test/** -text +/scripts/update-checks-doc/testdata/** -text diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 91316ba28..51c68feb3 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -104,8 +104,6 @@ jobs: echo "$diffs" >&2 exit 1 fi - - name: Check `docs/checks.md` is up-to-date - run: go run ./scripts/update-checks-doc -check ./docs/checks.md - name: Install staticcheck run: | go install honnef.co/go/tools/cmd/staticcheck@latest diff --git a/Makefile b/Makefile index b7aa8fe23..1bba72d5d 100644 --- a/Makefile +++ b/Makefile @@ -23,9 +23,10 @@ all: build test lint t test: .testtimestamp -.staticchecktimestamp: $(TESTS) $(SRCS) $(TOOL) +.staticchecktimestamp: $(TESTS) $(SRCS) $(TOOL) docs/checks.md staticcheck ./... GOOS=js GOARCH=wasm staticcheck ./playground + go run ./scripts/update-checks-doc -check -quiet docs/checks.md touch .staticchecktimestamp l lint: .staticchecktimestamp diff --git a/docs/checks.md b/docs/checks.md index bffdd0e0c..42d336679 100644 --- a/docs/checks.md +++ b/docs/checks.md @@ -2182,6 +2182,7 @@ jobs: ``` Output: + ``` test.yaml:6:5: when a reusable workflow is called with "uses", "runs-on" is not available. only following keys are allowed: "name", "uses", "with", "secrets", "needs", "if", and "permissions" in job "job1" [syntax-check] diff --git a/scripts/update-checks-doc/README.md b/scripts/update-checks-doc/README.md index a0e26993a..a384f9177 100644 --- a/scripts/update-checks-doc/README.md +++ b/scripts/update-checks-doc/README.md @@ -9,12 +9,9 @@ This script does: - update the links to the [playground](https://rhysd.github.io/actionlint/) for the example inputs - check the document is up-to-date -For making the implementation simple, this script does not support Windows. - ## Prerequisites - Go -- Linux or macOS - `shellcheck` command - `pyflakes` command diff --git a/scripts/update-checks-doc/main.go b/scripts/update-checks-doc/main.go index 85bc6732a..ae76e7661 100644 --- a/scripts/update-checks-doc/main.go +++ b/scripts/update-checks-doc/main.go @@ -11,6 +11,7 @@ import ( "io" "log" "os" + "runtime" "strings" "github.com/google/go-cmp/cmp" @@ -183,6 +184,7 @@ func (u *Updater) Update() { isInputHeader := l == "Example input:" isOutputHeader := l == "Output:" isSkipOutput := l == "" + isSkipOutputWin := l == "" isSkipPlaygroundLink := l == "" isPlaygroundLink := strings.HasPrefix(l, "[Playground](") && strings.HasSuffix(l, ")") @@ -194,7 +196,7 @@ func (u *Updater) Update() { u.expect(stateHeading, stateEnd) case isOutputHeader: u.expect(stateAfterInput) - case isSkipOutput: + case isSkipOutput, isSkipOutputWin: u.expect(stateOutputHeader) case isSkipPlaygroundLink, isPlaygroundLink: u.expect(stateAfterOutput) @@ -274,7 +276,7 @@ func (u *Updater) Update() { u.state(stateOutputHeader, "Found example output header") } case stateOutputHeader: - if isSkipOutput { + if isSkipOutput || isSkipOutputWin && runtime.GOOS == "windows" { u.state(stateAfterOutput, "Skip updating output due to the comment") } else if l == "```" { u.state(stateOutputBlock, "Start code block for output") @@ -317,8 +319,10 @@ var stderr io.Writer = os.Stderr func Main(args []string) error { var check bool + var quiet bool flags := flag.NewFlagSet(args[0], flag.ContinueOnError) flags.BoolVar(&check, "check", false, "check the document is up-to-date") + flags.BoolVar(&quiet, "quiet", false, "disable trace log") flags.SetOutput(stderr) flags.Usage = func() { fmt.Fprintln(stderr, "Usage: update-checks-doc [FLAGS] FILE\n\nFlags:") @@ -335,12 +339,20 @@ func Main(args []string) error { } path := flags.Arg(0) + if quiet { + log.SetOutput(io.Discard) + } + in, err := os.ReadFile(path) if err != nil { return fmt.Errorf("could not read the document file: %w", err) } log.Printf("Read %d bytes from %q", len(in), path) + if runtime.GOOS == "windows" { + in = bytes.ReplaceAll(in, []byte{'\r', '\n'}, []byte{'\n'}) + } + out, err := Update(in) if err != nil { return err diff --git a/scripts/update-checks-doc/main_test.go b/scripts/update-checks-doc/main_test.go index cd1ab51ab..b98f9abe9 100644 --- a/scripts/update-checks-doc/main_test.go +++ b/scripts/update-checks-doc/main_test.go @@ -40,9 +40,6 @@ func testErr(t *testing.T, err error, want ...string) { } func TestMainGenerateOK(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("update-checks-doc doesn't support Windows") - } root := t.TempDir() in := must(os.Open(filepath.FromSlash("testdata/ok/minimal.in"))) @@ -64,15 +61,19 @@ func TestMainGenerateOK(t *testing.T) { } func TestMainCheckOK(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("update-checks-doc doesn't support Windows") - } path := filepath.FromSlash("testdata/ok/minimal.out") if err := Main([]string{"exe", "-check", path}); err != nil { t.Fatal(err) } } +func TestMainCheckQuietOK(t *testing.T) { + path := filepath.FromSlash("testdata/ok/minimal.out") + if err := Main([]string{"exe", "-check", "-quiet", path}); err != nil { + t.Fatal(err) + } +} + func TestMainPrintHelp(t *testing.T) { if err := Main([]string{"exe", "-help"}); err != nil { t.Fatal(err) @@ -80,9 +81,6 @@ func TestMainPrintHelp(t *testing.T) { } func TestMainCheckError(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("update-checks-doc doesn't support Windows") - } path := filepath.FromSlash("testdata/ok/minimal.in") testErr(t, Main([]string{"exe", "-check", path}), "checks document has some update") } @@ -100,9 +98,6 @@ func TestMainInvalidCheckFlag(t *testing.T) { } func TestMainNoUpdate(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("update-checks-doc doesn't support Windows") - } path := filepath.FromSlash("testdata/ok/minimal.out") if err := Main([]string{"exe", path}); err != nil { t.Fatal(err) @@ -117,10 +112,6 @@ func TestMainUpdateError(t *testing.T) { } func TestUpdateOK(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("update-checks-doc doesn't support Windows") - } - dir := filepath.FromSlash("testdata/ok") tests := []string{} @@ -129,7 +120,14 @@ func TestUpdateOK(t *testing.T) { if !strings.HasSuffix(n, ".in") { continue } - tests = append(tests, strings.TrimSuffix(n, filepath.Ext(n))) + + id := strings.TrimSuffix(n, filepath.Ext(n)) + // This test case does not work on Windows + if runtime.GOOS == "windows" && id == "replace_absolute_path" { + continue + } + + tests = append(tests, id) } for _, tc := range tests {