Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 9fc415f

Browse files
authored
Merge pull request #124 from tbehling/offer-project-cwd
Allow ShellCheck `source=` directive to be relative to project root instead of file path
2 parents b600cb0 + c36f7c7 commit 9fc415f

File tree

6 files changed

+82
-6
lines changed

6 files changed

+82
-6
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,26 @@ instead prefer editing the configuration by hand you can get to that by editing
2828
`~/.atom/config.cson` (choose Open Your Config in Atom menu). The settings
2929
available are:
3030

31-
* `shellcheckExecutablePath`: The full path to the `shellcheck` executable.
32-
Run `which shellcheck` to find where it is installed on your system.
33-
34-
* `userParameters`: Any additional executable parameters to pass to
35-
`shellcheck` when linting your files.
31+
- `shellcheckExecutablePath`: The full path to the `shellcheck` executable.
32+
Run `which shellcheck` to find where it is installed on your system.
33+
34+
- `userParameters`: Any additional executable parameters to pass to
35+
`shellcheck` when linting your files.
36+
37+
- `enableNotice`: Include lesser-importance ShellCheck messages
38+
(default: false).
39+
40+
- `useProjectCwd`: Controls whether the paths used by ShellCheck's
41+
[`source=`](https://github.com/koalaman/shellcheck/wiki/Directive#source)
42+
directive are relative to the project root or the file (default: false, for
43+
file-relative)
44+
45+
- If true, ShellCheck's working directory
46+
will be the project's root directory. Any `source=` directives will be
47+
interpreted relative to the project root.
48+
49+
- Otherwise, ShellCheck will run relative to the file's directory,
50+
making `source=` directives file-relative.
3651

3752
[linter]: https://github.com/atom-community/linter "Linter"
3853
[shellcheck]: https://github.com/koalaman/shellcheck "ShellCheck"

lib/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export default {
2727
atom.config.observe('linter-shellcheck.userParameters', (value) => {
2828
this.userParameters = value.trim().split(' ').filter(Boolean);
2929
}),
30+
atom.config.observe('linter-shellcheck.useProjectCwd', (value) => {
31+
this.useProjectCwd = value;
32+
}),
3033
);
3134
},
3235

@@ -61,7 +64,8 @@ export default {
6164
}
6265

6366
const text = textEditor.getText();
64-
const cwd = path.dirname(filePath);
67+
const projectPath = atom.project.relativizePath(filePath)[0];
68+
const cwd = this.useProjectCwd && projectPath ? projectPath : path.dirname(filePath);
6569
const showAll = this.enableNotice;
6670
// The first -f parameter overrides any others
6771
const parameters = [].concat(['-f', 'gcc'], this.userParameters, ['-']);

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
"type": "boolean",
3232
"title": "Enable Notice Messages",
3333
"default": false
34+
},
35+
"useProjectCwd": {
36+
"type": "boolean",
37+
"title": "Run ShellCheck relative to Project Root",
38+
"description": "Enable to run ShellCheck using the project root as its working directory; causes ShellCheck to interpret `source=` paths relative to the project root. Disable to keep `source=` paths relative to the file.",
39+
"default": false
3440
}
3541
},
3642
"dependencies": {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
# "project root" in the test suite is spec/fixtures/
4+
#shellcheck source=../clean.sh
5+
. clean.sh
6+
echo "file relative"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
# "project root" in the test suite is spec/fixtures/
4+
#shellcheck source=clean.sh
5+
. clean.sh
6+
echo "project relative"

spec/linter-shellcheck-spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const { lint } = require('../lib/main.js').provideLinter();
88

99
const cleanPath = path.join(__dirname, 'fixtures', 'clean.sh');
1010
const badPath = path.join(__dirname, 'fixtures', 'bad.sh');
11+
const sourceFileRelativePath = path.join(__dirname, 'fixtures', 'source_directive', 'file_relative.sh');
12+
const sourceProjectRelativePath = path.join(__dirname, 'fixtures', 'source_directive', 'project_relative.sh');
1113

1214
describe('The ShellCheck provider for Linter', () => {
1315
beforeEach(async () => {
@@ -44,4 +46,41 @@ describe('The ShellCheck provider for Linter', () => {
4446
expect(messages[0].filePath).toBe(badPath);
4547
expect(messages[0].range).toEqual([[0, 0], [0, 4]]);
4648
});
49+
50+
describe('implements useProjectCwd and', () => {
51+
beforeEach(async () => {
52+
atom.config.set('linter-shellcheck.userParameters', '-x');
53+
atom.config.set('linter-shellcheck.enableNotice', true);
54+
});
55+
56+
it('uses file-relative source= directives by default', async () => {
57+
atom.config.set('linter-shellcheck.useProjectCwd', false);
58+
const editor = await atom.workspace.open(sourceFileRelativePath);
59+
const messages = await lint(editor);
60+
expect(messages.length).toBe(0);
61+
});
62+
63+
it('errors for file-relative source= path with useProjectCwd = true', async () => {
64+
atom.config.set('linter-shellcheck.useProjectCwd', true);
65+
const editor = await atom.workspace.open(sourceFileRelativePath);
66+
const messages = await lint(editor);
67+
expect(messages.length).toBe(1);
68+
expect(messages[0].html).toMatch(/openBinaryFile: does not exist/);
69+
});
70+
71+
it('uses project-relative source= directives via setting (based at fixtures/)', async () => {
72+
atom.config.set('linter-shellcheck.useProjectCwd', true);
73+
const editor = await atom.workspace.open(sourceProjectRelativePath);
74+
const messages = await lint(editor);
75+
expect(messages.length).toBe(0);
76+
});
77+
78+
it('errors for project-relative source= path with useProjectCwd = false (based at fixtures/)', async () => {
79+
atom.config.set('linter-shellcheck.useProjectCwd', false);
80+
const editor = await atom.workspace.open(sourceProjectRelativePath);
81+
const messages = await lint(editor);
82+
expect(messages.length).toBe(1);
83+
expect(messages[0].html).toMatch(/openBinaryFile: does not exist/);
84+
});
85+
});
4786
});

0 commit comments

Comments
 (0)