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

Commit

Permalink
Merge pull request #124 from tbehling/offer-project-cwd
Browse files Browse the repository at this point in the history
Allow ShellCheck `source=` directive to be relative to project root instead of file path
  • Loading branch information
Arcanemagus authored Aug 23, 2018
2 parents b600cb0 + c36f7c7 commit 9fc415f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ instead prefer editing the configuration by hand you can get to that by editing
`~/.atom/config.cson` (choose Open Your Config in Atom menu). The settings
available are:

* `shellcheckExecutablePath`: The full path to the `shellcheck` executable.
Run `which shellcheck` to find where it is installed on your system.

* `userParameters`: Any additional executable parameters to pass to
`shellcheck` when linting your files.
- `shellcheckExecutablePath`: The full path to the `shellcheck` executable.
Run `which shellcheck` to find where it is installed on your system.

- `userParameters`: Any additional executable parameters to pass to
`shellcheck` when linting your files.

- `enableNotice`: Include lesser-importance ShellCheck messages
(default: false).

- `useProjectCwd`: Controls whether the paths used by ShellCheck's
[`source=`](https://github.com/koalaman/shellcheck/wiki/Directive#source)
directive are relative to the project root or the file (default: false, for
file-relative)

- If true, ShellCheck's working directory
will be the project's root directory. Any `source=` directives will be
interpreted relative to the project root.

- Otherwise, ShellCheck will run relative to the file's directory,
making `source=` directives file-relative.

[linter]: https://github.com/atom-community/linter "Linter"
[shellcheck]: https://github.com/koalaman/shellcheck "ShellCheck"
6 changes: 5 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export default {
atom.config.observe('linter-shellcheck.userParameters', (value) => {
this.userParameters = value.trim().split(' ').filter(Boolean);
}),
atom.config.observe('linter-shellcheck.useProjectCwd', (value) => {
this.useProjectCwd = value;
}),
);
},

Expand Down Expand Up @@ -61,7 +64,8 @@ export default {
}

const text = textEditor.getText();
const cwd = path.dirname(filePath);
const projectPath = atom.project.relativizePath(filePath)[0];
const cwd = this.useProjectCwd && projectPath ? projectPath : path.dirname(filePath);
const showAll = this.enableNotice;
// The first -f parameter overrides any others
const parameters = [].concat(['-f', 'gcc'], this.userParameters, ['-']);
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
"type": "boolean",
"title": "Enable Notice Messages",
"default": false
},
"useProjectCwd": {
"type": "boolean",
"title": "Run ShellCheck relative to Project Root",
"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.",
"default": false
}
},
"dependencies": {
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/source_directive/file_relative.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# "project root" in the test suite is spec/fixtures/
#shellcheck source=../clean.sh
. clean.sh
echo "file relative"
6 changes: 6 additions & 0 deletions spec/fixtures/source_directive/project_relative.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

# "project root" in the test suite is spec/fixtures/
#shellcheck source=clean.sh
. clean.sh
echo "project relative"
39 changes: 39 additions & 0 deletions spec/linter-shellcheck-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { lint } = require('../lib/main.js').provideLinter();

const cleanPath = path.join(__dirname, 'fixtures', 'clean.sh');
const badPath = path.join(__dirname, 'fixtures', 'bad.sh');
const sourceFileRelativePath = path.join(__dirname, 'fixtures', 'source_directive', 'file_relative.sh');
const sourceProjectRelativePath = path.join(__dirname, 'fixtures', 'source_directive', 'project_relative.sh');

describe('The ShellCheck provider for Linter', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -44,4 +46,41 @@ describe('The ShellCheck provider for Linter', () => {
expect(messages[0].filePath).toBe(badPath);
expect(messages[0].range).toEqual([[0, 0], [0, 4]]);
});

describe('implements useProjectCwd and', () => {
beforeEach(async () => {
atom.config.set('linter-shellcheck.userParameters', '-x');
atom.config.set('linter-shellcheck.enableNotice', true);
});

it('uses file-relative source= directives by default', async () => {
atom.config.set('linter-shellcheck.useProjectCwd', false);
const editor = await atom.workspace.open(sourceFileRelativePath);
const messages = await lint(editor);
expect(messages.length).toBe(0);
});

it('errors for file-relative source= path with useProjectCwd = true', async () => {
atom.config.set('linter-shellcheck.useProjectCwd', true);
const editor = await atom.workspace.open(sourceFileRelativePath);
const messages = await lint(editor);
expect(messages.length).toBe(1);
expect(messages[0].html).toMatch(/openBinaryFile: does not exist/);
});

it('uses project-relative source= directives via setting (based at fixtures/)', async () => {
atom.config.set('linter-shellcheck.useProjectCwd', true);
const editor = await atom.workspace.open(sourceProjectRelativePath);
const messages = await lint(editor);
expect(messages.length).toBe(0);
});

it('errors for project-relative source= path with useProjectCwd = false (based at fixtures/)', async () => {
atom.config.set('linter-shellcheck.useProjectCwd', false);
const editor = await atom.workspace.open(sourceProjectRelativePath);
const messages = await lint(editor);
expect(messages.length).toBe(1);
expect(messages[0].html).toMatch(/openBinaryFile: does not exist/);
});
});
});

0 comments on commit 9fc415f

Please sign in to comment.