Skip to content

Commit

Permalink
add support for Windows to update-checks-doc script
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 24, 2024
1 parent 54f7d98 commit 6807526
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,7 @@ jobs:
```

Output:
<!-- Skip update output on Windows -->

```
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]
Expand Down
3 changes: 0 additions & 3 deletions scripts/update-checks-doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 19 additions & 3 deletions scripts/update-checks-doc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"log"
"os"
"runtime"
"strings"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -183,6 +184,7 @@ func (u *Updater) Update() {
isInputHeader := l == "Example input:"
isOutputHeader := l == "Output:"
isSkipOutput := l == "<!-- Skip update output -->"
isSkipOutputWin := l == "<!-- Skip update output on Windows -->"
isSkipPlaygroundLink := l == "<!-- Skip playground link -->"
isPlaygroundLink := strings.HasPrefix(l, "[Playground](") && strings.HasSuffix(l, ")")

Expand All @@ -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)
Expand Down Expand Up @@ -274,8 +276,12 @@ func (u *Updater) Update() {
u.state(stateOutputHeader, "Found example output header")
}
case stateOutputHeader:
if isSkipOutput {
u.state(stateAfterOutput, "Skip updating output due to the comment")
if isSkipOutput || isSkipOutputWin && runtime.GOOS == "windows" {
reason := "Skip updating output due to the comment"
if isSkipOutputWin {
reason += " only on Windows"
}
u.state(stateAfterOutput, reason)
} else if l == "```" {
u.state(stateOutputBlock, "Start code block for output")
}
Expand Down Expand Up @@ -317,8 +323,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:")
Expand All @@ -335,12 +343,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
Expand Down
36 changes: 19 additions & 17 deletions scripts/update-checks-doc/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -64,25 +61,26 @@ 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)
}
}

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")
}
Expand All @@ -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)
Expand All @@ -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{}
Expand All @@ -129,7 +120,18 @@ 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
}
// This test case only works on Windows
if runtime.GOOS != "windows" && id == "skip_output_on_windows" {
continue
}

tests = append(tests, id)
}

for _, tc := range tests {
Expand Down
49 changes: 49 additions & 0 deletions scripts/update-checks-doc/testdata/ok/skip_output_on_windows.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<a id="hello"></a>
## Hello

Example input:

```yaml
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo ${{ unknown }}
```

Output:
<!-- Skip update output on Windows -->

```
This section will NOT be updated
```

[Playground](URL_WILL_BE_GENERATED)

Skip updating the output.

<a id="hello2"></a>
## Hello 2

Example input:

```yaml
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo ${{ unknown }}
```

Output:
<!-- Skip update output on Windows -->

```
This section will NOT be updated
```

[Playground](URL_WILL_BE_GENERATED)

Skip updating the output.
49 changes: 49 additions & 0 deletions scripts/update-checks-doc/testdata/ok/skip_output_on_windows.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<a id="hello"></a>
## Hello

Example input:

```yaml
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo ${{ unknown }}
```

Output:
<!-- Skip update output -->

```
This section will NOT be updated
```

[Playground](https://rhysd.github.io/actionlint/#eNokyjEOhSAQRdGeVbzit2yA3XwMCVEzQ5x5sSDs3YxWtzhXpWDQetq1WkmAN/MocFEsh7NSnPn8h71k3oZ9F5DjLGhbV/zmBOUQvQVrPQEAAP//SLkdHQ==)

Skip updating the output.

<a id="hello2"></a>
## Hello 2

Example input:

```yaml
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo ${{ unknown }}
```

Output:
<!-- Skip update output -->

```
This section will NOT be updated
```

[Playground](https://rhysd.github.io/actionlint/#eNokyjEOhSAQRdGeVbzit2yA3XwMCVEzQ5x5sSDs3YxWtzhXpWDQetq1WkmAN/MocFEsh7NSnPn8h71k3oZ9F5DjLGhbV/zmBOUQvQVrPQEAAP//SLkdHQ==)

Skip updating the output.

0 comments on commit 6807526

Please sign in to comment.