Skip to content

Commit d2750d4

Browse files
committed
detect duplicate id and section title by update-checks-doc script
1 parent 141b363 commit d2750d4

File tree

9 files changed

+236
-1
lines changed

9 files changed

+236
-1
lines changed

scripts/update-checks-doc/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func Update(in []byte) ([]byte, error) {
9090
var inInput bool
9191
var skipOutput bool
9292
var count int
93-
lnum := 0
93+
var lnum int
94+
anchors := map[string]int{}
95+
sections := map[string]int{}
9496
scan := bufio.NewScanner(bytes.NewReader(in))
9597
for scan.Scan() {
9698
lnum++
@@ -104,6 +106,10 @@ func Update(in []byte) ([]byte, error) {
104106
}
105107
section = l[3:]
106108
log.Printf("Entering new section %q (%s) at line %d", section, anchor, lnum)
109+
if n, ok := sections[section]; ok {
110+
return nil, fmt.Errorf("section %q at line %d was already used at line %d", section, lnum, n)
111+
}
112+
sections[section] = lnum
107113
anchor = ""
108114
inputHeader = false
109115
outputHeader = false
@@ -116,6 +122,10 @@ func Update(in []byte) ([]byte, error) {
116122
if len(anchor) == 0 {
117123
return nil, fmt.Errorf("id for <a> tag is empty at line %d", lnum)
118124
}
125+
if n, ok := anchors[anchor]; ok {
126+
return nil, fmt.Errorf("id %q at line %d was already used at line %d", anchor, lnum, n)
127+
}
128+
anchors[anchor] = lnum
119129
}
120130
if l == "Example input:" {
121131
log.Printf("Found example input header for %q at line %d", section, lnum)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<a id="hello"></a>
2+
## Hello
3+
4+
Example input:
5+
6+
```yaml
7+
on: push
8+
```
9+
10+
Output:
11+
12+
```
13+
This block will be auto-generated
14+
```
15+
16+
[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED)
17+
18+
This section causes no error.
19+
20+
<a id="hello"></a>
21+
## Hello 2
22+
23+
Example input:
24+
25+
```yaml
26+
on: push
27+
```
28+
29+
Output:
30+
31+
```
32+
This block will be auto-generated
33+
```
34+
35+
[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED)
36+
37+
This section causes an error since same `id` is used.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<a id="hello"></a>
2+
## Hello
3+
4+
Example input:
5+
6+
```yaml
7+
on: push
8+
```
9+
10+
Output:
11+
12+
```
13+
This block will be auto-generated
14+
```
15+
16+
[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED)
17+
18+
This section causes no error.
19+
20+
<a id="hello2"></a>
21+
## Hello
22+
23+
Example input:
24+
25+
```yaml
26+
on: push
27+
```
28+
29+
Output:
30+
31+
```
32+
This block will be auto-generated
33+
```
34+
35+
[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED)
36+
37+
This section causes an error since same section title is used.

scripts/update-checks-doc/testdata/ok/skip_output.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,28 @@ This section will NOT be updated
2222
[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED)
2323

2424
Skip updating the output.
25+
26+
<a id="hello2"></a>
27+
## Hello 2
28+
29+
Example input:
30+
31+
```yaml
32+
on: push
33+
jobs:
34+
test:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- run: echo ${{ unknown }}
38+
```
39+
40+
Output:
41+
<!-- Skip update output -->
42+
43+
```
44+
This section will NOT be updated
45+
```
46+
47+
[Playground](https://rhysd.github.io/actionlint/#THIS_URL_WILL_BE_UPDATED)
48+
49+
Skip updating the output.

scripts/update-checks-doc/testdata/ok/skip_output.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,28 @@ This section will NOT be updated
2222
[Playground](https://rhysd.github.io/actionlint/#eNokyjEOhSAQRdGeVbzit2yA3XwMCVEzQ5x5sSDs3YxWtzhXpWDQetq1WkmAN/MocFEsh7NSnPn8h71k3oZ9F5DjLGhbV/zmBOUQvQVrPQEAAP//SLkdHQ==)
2323

2424
Skip updating the output.
25+
26+
<a id="hello2"></a>
27+
## Hello 2
28+
29+
Example input:
30+
31+
```yaml
32+
on: push
33+
jobs:
34+
test:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- run: echo ${{ unknown }}
38+
```
39+
40+
Output:
41+
<!-- Skip update output -->
42+
43+
```
44+
This section will NOT be updated
45+
```
46+
47+
[Playground](https://rhysd.github.io/actionlint/#eNokyjEOhSAQRdGeVbzit2yA3XwMCVEzQ5x5sSDs3YxWtzhXpWDQetq1WkmAN/MocFEsh7NSnPn8h71k3oZ9F5DjLGhbV/zmBOUQvQVrPQEAAP//SLkdHQ==)
48+
49+
Skip updating the output.

scripts/update-checks-doc/testdata/ok/skip_output_and_playground.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,28 @@ This section will NOT be updated
2222
<!-- Skip playground link -->
2323

2424
Skip both the output and the playground link.
25+
26+
<a id="hello2"></a>
27+
## Hello 2
28+
29+
Example input:
30+
31+
```yaml
32+
on: push
33+
jobs:
34+
test:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- run: echo ${{ unknown }}
38+
```
39+
40+
Output:
41+
<!-- Skip update output -->
42+
43+
```
44+
This section will NOT be updated
45+
```
46+
47+
<!-- Skip playground link -->
48+
49+
Skip both the output and the playground link.

scripts/update-checks-doc/testdata/ok/skip_output_and_playground.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,28 @@ This section will NOT be updated
2222
<!-- Skip playground link -->
2323

2424
Skip both the output and the playground link.
25+
26+
<a id="hello2"></a>
27+
## Hello 2
28+
29+
Example input:
30+
31+
```yaml
32+
on: push
33+
jobs:
34+
test:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- run: echo ${{ unknown }}
38+
```
39+
40+
Output:
41+
<!-- Skip update output -->
42+
43+
```
44+
This section will NOT be updated
45+
```
46+
47+
<!-- Skip playground link -->
48+
49+
Skip both the output and the playground link.

scripts/update-checks-doc/testdata/ok/skip_playground.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ This section will be generated
2121
<!-- Skip playground link -->
2222

2323
This skips playground link.
24+
25+
<a id="hello2"></a>
26+
## Hello 2
27+
28+
Example input:
29+
30+
```yaml
31+
on: push
32+
jobs:
33+
test:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- run: echo ${{ unknown }}
37+
```
38+
39+
Output:
40+
41+
```
42+
This section will be generated
43+
```
44+
45+
<!-- Skip playground link -->
46+
47+
This skips playground link.

scripts/update-checks-doc/testdata/ok/skip_playground.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,30 @@ test.yaml:6:23: undefined variable "unknown". available variables are "env", "gi
2424
<!-- Skip playground link -->
2525

2626
This skips playground link.
27+
28+
<a id="hello2"></a>
29+
## Hello 2
30+
31+
Example input:
32+
33+
```yaml
34+
on: push
35+
jobs:
36+
test:
37+
runs-on: ubuntu-latest
38+
steps:
39+
- run: echo ${{ unknown }}
40+
```
41+
42+
Output:
43+
44+
```
45+
test.yaml:6:23: undefined variable "unknown". available variables are "env", "github", "inputs", "job", "matrix", "needs", "runner", "secrets", "steps", "strategy", "vars" [expression]
46+
|
47+
6 | - run: echo ${{ unknown }}
48+
| ^~~~~~~
49+
```
50+
51+
<!-- Skip playground link -->
52+
53+
This skips playground link.

0 commit comments

Comments
 (0)