Skip to content

Commit

Permalink
detect duplicate id and section title by update-checks-doc script
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Oct 21, 2024
1 parent 141b363 commit d2750d4
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 1 deletion.
12 changes: 11 additions & 1 deletion scripts/update-checks-doc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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
Expand All @@ -116,6 +122,10 @@ func Update(in []byte) ([]byte, error) {
if len(anchor) == 0 {
return nil, fmt.Errorf("id for <a> 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)
Expand Down
37 changes: 37 additions & 0 deletions scripts/update-checks-doc/testdata/err/duplicate_id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<a id="hello"></a>
## 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.

<a id="hello"></a>
## 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.
37 changes: 37 additions & 0 deletions scripts/update-checks-doc/testdata/err/duplicate_title.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<a id="hello"></a>
## 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.

<a id="hello2"></a>
## 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.
25 changes: 25 additions & 0 deletions scripts/update-checks-doc/testdata/ok/skip_output.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<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/#THIS_URL_WILL_BE_UPDATED)

Skip updating the output.
25 changes: 25 additions & 0 deletions scripts/update-checks-doc/testdata/ok/skip_output.out
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ 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.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ This section will NOT be updated
<!-- Skip playground link -->

Skip both the output and the playground link.

<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
```

<!-- Skip playground link -->

Skip both the output and the playground link.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,28 @@ This section will NOT be updated
<!-- Skip playground link -->

Skip both the output and the playground link.

<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
```

<!-- Skip playground link -->

Skip both the output and the playground link.
24 changes: 24 additions & 0 deletions scripts/update-checks-doc/testdata/ok/skip_playground.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ This section will be generated
<!-- Skip playground link -->

This skips playground link.

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

Example input:

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

Output:

```
This section will be generated
```

<!-- Skip playground link -->

This skips playground link.
27 changes: 27 additions & 0 deletions scripts/update-checks-doc/testdata/ok/skip_playground.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,30 @@ test.yaml:6:23: undefined variable "unknown". available variables are "env", "gi
<!-- Skip playground link -->

This skips playground link.

<a id="hello2"></a>
## 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 }}
| ^~~~~~~
```

<!-- Skip playground link -->

This skips playground link.

0 comments on commit d2750d4

Please sign in to comment.