From d2750d49e93bcaf04c5cb4eb01a28bd2ca0d6aa1 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 21 Oct 2024 14:53:52 +0900 Subject: [PATCH] detect duplicate `id` and section title by `update-checks-doc` script --- scripts/update-checks-doc/main.go | 12 +++++- .../testdata/err/duplicate_id.md | 37 +++++++++++++++++++ .../testdata/err/duplicate_title.md | 37 +++++++++++++++++++ .../testdata/ok/skip_output.in | 25 +++++++++++++ .../testdata/ok/skip_output.out | 25 +++++++++++++ .../testdata/ok/skip_output_and_playground.in | 25 +++++++++++++ .../ok/skip_output_and_playground.out | 25 +++++++++++++ .../testdata/ok/skip_playground.in | 24 ++++++++++++ .../testdata/ok/skip_playground.out | 27 ++++++++++++++ 9 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 scripts/update-checks-doc/testdata/err/duplicate_id.md create mode 100644 scripts/update-checks-doc/testdata/err/duplicate_title.md diff --git a/scripts/update-checks-doc/main.go b/scripts/update-checks-doc/main.go index dc8fb43ed..05194977a 100644 --- a/scripts/update-checks-doc/main.go +++ b/scripts/update-checks-doc/main.go @@ -90,7 +90,9 @@ func Update(in []byte) ([]byte, error) { var inInput bool var skipOutput bool var count int - lnum := 0 + var lnum int + anchors := map[string]int{} + sections := map[string]int{} scan := bufio.NewScanner(bytes.NewReader(in)) for scan.Scan() { lnum++ @@ -104,6 +106,10 @@ func Update(in []byte) ([]byte, error) { } section = l[3:] log.Printf("Entering new section %q (%s) at line %d", section, anchor, lnum) + if n, ok := sections[section]; ok { + return nil, fmt.Errorf("section %q at line %d was already used at line %d", section, lnum, n) + } + sections[section] = lnum anchor = "" inputHeader = false outputHeader = false @@ -116,6 +122,10 @@ func Update(in []byte) ([]byte, error) { if len(anchor) == 0 { return nil, fmt.Errorf("id for tag is empty at line %d", lnum) } + if n, ok := anchors[anchor]; ok { + return nil, fmt.Errorf("id %q at line %d was already used at line %d", anchor, lnum, n) + } + anchors[anchor] = lnum } if l == "Example input:" { log.Printf("Found example input header for %q at line %d", section, lnum) diff --git a/scripts/update-checks-doc/testdata/err/duplicate_id.md b/scripts/update-checks-doc/testdata/err/duplicate_id.md new file mode 100644 index 000000000..e4d4c67f6 --- /dev/null +++ b/scripts/update-checks-doc/testdata/err/duplicate_id.md @@ -0,0 +1,37 @@ + +## Hello + +Example input: + +```yaml +on: push +``` + +Output: + +``` +This block will be auto-generated +``` + +[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED) + +This section causes no error. + + +## Hello 2 + +Example input: + +```yaml +on: push +``` + +Output: + +``` +This block will be auto-generated +``` + +[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED) + +This section causes an error since same `id` is used. diff --git a/scripts/update-checks-doc/testdata/err/duplicate_title.md b/scripts/update-checks-doc/testdata/err/duplicate_title.md new file mode 100644 index 000000000..41e869e19 --- /dev/null +++ b/scripts/update-checks-doc/testdata/err/duplicate_title.md @@ -0,0 +1,37 @@ + +## Hello + +Example input: + +```yaml +on: push +``` + +Output: + +``` +This block will be auto-generated +``` + +[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED) + +This section causes no error. + + +## Hello + +Example input: + +```yaml +on: push +``` + +Output: + +``` +This block will be auto-generated +``` + +[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED) + +This section causes an error since same section title is used. diff --git a/scripts/update-checks-doc/testdata/ok/skip_output.in b/scripts/update-checks-doc/testdata/ok/skip_output.in index b19e899de..9df9dfb3a 100644 --- a/scripts/update-checks-doc/testdata/ok/skip_output.in +++ b/scripts/update-checks-doc/testdata/ok/skip_output.in @@ -22,3 +22,28 @@ This section will NOT be updated [Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED) Skip updating the output. + + +## Hello 2 + +Example input: + +```yaml +on: push +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo ${{ unknown }} +``` + +Output: + + +``` +This section will NOT be updated +``` + +[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED) + +Skip updating the output. diff --git a/scripts/update-checks-doc/testdata/ok/skip_output.out b/scripts/update-checks-doc/testdata/ok/skip_output.out index 0921ecfc4..930d7d795 100644 --- a/scripts/update-checks-doc/testdata/ok/skip_output.out +++ b/scripts/update-checks-doc/testdata/ok/skip_output.out @@ -22,3 +22,28 @@ This section will NOT be updated [Playground](https://rhysd.github.io/actionlint/#eNokyjEOhSAQRdGeVbzit2yA3XwMCVEzQ5x5sSDs3YxWtzhXpWDQetq1WkmAN/MocFEsh7NSnPn8h71k3oZ9F5DjLGhbV/zmBOUQvQVrPQEAAP//SLkdHQ==) Skip updating the output. + + +## Hello 2 + +Example input: + +```yaml +on: push +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo ${{ unknown }} +``` + +Output: + + +``` +This section will NOT be updated +``` + +[Playground](https://rhysd.github.io/actionlint/#eNokyjEOhSAQRdGeVbzit2yA3XwMCVEzQ5x5sSDs3YxWtzhXpWDQetq1WkmAN/MocFEsh7NSnPn8h71k3oZ9F5DjLGhbV/zmBOUQvQVrPQEAAP//SLkdHQ==) + +Skip updating the output. diff --git a/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.in b/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.in index 18a83d7e6..87b115305 100644 --- a/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.in +++ b/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.in @@ -22,3 +22,28 @@ This section will NOT be updated Skip both the output and the playground link. + + +## Hello 2 + +Example input: + +```yaml +on: push +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo ${{ unknown }} +``` + +Output: + + +``` +This section will NOT be updated +``` + + + +Skip both the output and the playground link. diff --git a/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.out b/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.out index 18a83d7e6..87b115305 100644 --- a/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.out +++ b/scripts/update-checks-doc/testdata/ok/skip_output_and_playground.out @@ -22,3 +22,28 @@ This section will NOT be updated Skip both the output and the playground link. + + +## Hello 2 + +Example input: + +```yaml +on: push +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo ${{ unknown }} +``` + +Output: + + +``` +This section will NOT be updated +``` + + + +Skip both the output and the playground link. diff --git a/scripts/update-checks-doc/testdata/ok/skip_playground.in b/scripts/update-checks-doc/testdata/ok/skip_playground.in index 28307c2e3..80a03b434 100644 --- a/scripts/update-checks-doc/testdata/ok/skip_playground.in +++ b/scripts/update-checks-doc/testdata/ok/skip_playground.in @@ -21,3 +21,27 @@ This section will be generated This skips playground link. + + +## Hello 2 + +Example input: + +```yaml +on: push +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo ${{ unknown }} +``` + +Output: + +``` +This section will be generated +``` + + + +This skips playground link. diff --git a/scripts/update-checks-doc/testdata/ok/skip_playground.out b/scripts/update-checks-doc/testdata/ok/skip_playground.out index 7d57c0e05..7bbc3521f 100644 --- a/scripts/update-checks-doc/testdata/ok/skip_playground.out +++ b/scripts/update-checks-doc/testdata/ok/skip_playground.out @@ -24,3 +24,30 @@ test.yaml:6:23: undefined variable "unknown". available variables are "env", "gi This skips playground link. + + +## Hello 2 + +Example input: + +```yaml +on: push +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: echo ${{ unknown }} +``` + +Output: + +``` +test.yaml:6:23: undefined variable "unknown". available variables are "env", "github", "inputs", "job", "matrix", "needs", "runner", "secrets", "steps", "strategy", "vars" [expression] + | +6 | - run: echo ${{ unknown }} + | ^~~~~~~ +``` + + + +This skips playground link.