Skip to content

Commit 17f5743

Browse files
committed
Add initial code
1 parent bfcb5fd commit 17f5743

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM alpine:latest
2+
3+
ADD sphinx_matcher.json .
4+
5+
ENTRYPOINT ["/bin/echo", "::add-matcher::sphinx_matcher.json"]

action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Sphinx Problem Matcher
2+
description: Attaches a problem matcher that looks for errors during Sphinx builds
3+
author: Ammar Askar
4+
branding:
5+
icon: book
6+
color: yellow
7+
runs:
8+
using: 'docker'
9+
image: 'Dockerfile

sphinx_matcher.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "sphinx-problem-matcher",
5+
"pattern": [
6+
{
7+
"regexp": "^(.*):(\\d*):\\s+(\\w*):\\s+(.*)$",
8+
"file": 1,
9+
"line": 2,
10+
"severity": 3,
11+
"message": 4
12+
},
13+
{
14+
"_comment": "A bit of a looser pattern, doesn't look for line numbers, just looks for file names relying on them to start with / and end with .rst",
15+
"regexp": "(\/.*\\.rst):\\s+(\\w*):\\s+(.*)$",
16+
"file": 1,
17+
"severity": 2,
18+
"message": 3
19+
}
20+
]
21+
}
22+
]
23+
}

test_matcher.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var assert = require('assert');
2+
var fs = require('fs');
3+
4+
const matcherJSON = fs.readFileSync('sphinx_matcher.json');
5+
const matcher = JSON.parse(matcherJSON);
6+
7+
const patterns = matcher.problemMatcher[0].pattern;
8+
9+
for (const pattern of patterns) {
10+
console.log("Patterns under test: ", pattern.regexp);
11+
}
12+
13+
const sphinx_log =
14+
`/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings_and_errors/index.rst:16: WARNING: Error in "code-block" directive:
15+
maximum 1 argument(s) allowed, 2 supplied.
16+
.. code-block:: ruby
17+
as
18+
19+
/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings/index.rst:22: WARNING: Problems with "include" directive path:
20+
InputError: [Errno 2] No such file or directory: 'I_DONT_EXIST'.
21+
22+
23+
/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings/index.rst:24: WARNING: Unknown directive type "BADDIRECTIVE".
24+
.. BADDIRECTIVE:: asdf
25+
26+
checking consistency... /home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings/notintoc.rst: WARNING: document isn't included in any toctree`;
27+
28+
29+
const expected_matches = [
30+
{
31+
'file': '/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings_and_errors/index.rst',
32+
'line': '16',
33+
'severity': 'WARNING',
34+
'message': 'Error in "code-block" directive:'
35+
},
36+
{
37+
'file': '/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings/index.rst',
38+
'line': '22',
39+
'severity': 'WARNING',
40+
'message': 'Problems with "include" directive path:'
41+
},
42+
{
43+
'file': '/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings/index.rst',
44+
'line': '24',
45+
'severity': 'WARNING',
46+
'message': 'Unknown directive type "BADDIRECTIVE".',
47+
},
48+
{
49+
'file': '/home/travis/build/ammaraskar/sphinx-action/tests/test_projects/warnings/notintoc.rst',
50+
'line': null,
51+
'severity': 'WARNING',
52+
'message': "document isn't included in any toctree"
53+
}
54+
]
55+
56+
function perform_match(pattern_object, line) {
57+
const match = line.match(pattern_object.regexp);
58+
59+
if (!match) {
60+
return null;
61+
}
62+
63+
return {
64+
file: pattern_object.file ? match[pattern_object.file] : null,
65+
line: pattern_object.line ? match[pattern_object.line] : null,
66+
severity: pattern_object.severity ? match[pattern_object.severity] : null,
67+
message: pattern_object.message ? match[pattern_object.message] : null
68+
};
69+
}
70+
71+
let matches = [];
72+
for (const line of sphinx_log.split(/\n/)) {
73+
for (const pattern_object of patterns) {
74+
const match = perform_match(pattern_object, line);
75+
if (match) {
76+
matches.push(match);
77+
}
78+
}
79+
}
80+
81+
console.log("Matches: ", matches);
82+
console.log("Expected matches: ", expected_matches);
83+
assert.deepEqual(expected_matches, matches);
84+
85+
console.log("[x] All good!");

0 commit comments

Comments
 (0)