@@ -2,6 +2,7 @@ import * as fs from 'fs'
2
2
import * as os from 'os'
3
3
import * as Parser from 'web-tree-sitter'
4
4
5
+ import { REPO_ROOT_FOLDER } from '../../../../testing/fixtures'
5
6
import { initializeParser } from '../../parser'
6
7
import { getSourceCommands } from '../sourcing'
7
8
@@ -13,8 +14,6 @@ beforeAll(async () => {
13
14
parser = await initializeParser ( )
14
15
} )
15
16
16
- jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( ) => true )
17
-
18
17
// mock os.homedir() to return a fixed path
19
18
jest . spyOn ( os , 'homedir' ) . mockImplementation ( ( ) => '/Users/bash-user' )
20
19
@@ -29,7 +28,9 @@ describe('getSourcedUris', () => {
29
28
expect ( sourceCommands ) . toEqual ( [ ] )
30
29
} )
31
30
32
- it ( 'returns a set of sourced files' , ( ) => {
31
+ it ( 'returns a set of sourced files (but ignores some unhandled cases)' , ( ) => {
32
+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( ) => true )
33
+
33
34
const fileContent = `
34
35
source file-in-path.sh # does not contain a slash (i.e. is maybe somewhere on the path)
35
36
@@ -73,7 +74,7 @@ describe('getSourcedUris', () => {
73
74
done
74
75
75
76
# ======================================
76
- # example of sourcing through a function
77
+ # Example of sourcing through a function
77
78
# ======================================
78
79
79
80
loadlib () {
@@ -149,4 +150,67 @@ describe('getSourcedUris', () => {
149
150
150
151
expect ( sourceCommands ) . toMatchSnapshot ( )
151
152
} )
153
+
154
+ it ( 'returns a set of sourced files and parses ShellCheck directives' , ( ) => {
155
+ jest . restoreAllMocks ( )
156
+
157
+ const fileContent = `
158
+ . ./scripts/release-client.sh
159
+
160
+ source ./testing/fixtures/issue206.sh
161
+
162
+ # shellcheck source=/dev/null
163
+ source ./IM_NOT_THERE.sh
164
+
165
+ # shellcheck source-path=testing/fixtures
166
+ source missing-node.sh # source path by directive
167
+
168
+ # shellcheck source=./testing/fixtures/install.sh
169
+ source "$X" # source by directive
170
+
171
+ # shellcheck source=./some-file-that-does-not-exist.sh
172
+ source "$Y" # not source due to invalid directive
173
+
174
+ # shellcheck source-path=SCRIPTDIR # note that this is already the behaviour of bash language server
175
+ source ./testing/fixtures/issue101.sh
176
+ `
177
+
178
+ const sourceCommands = getSourceCommands ( {
179
+ fileUri,
180
+ rootPath : REPO_ROOT_FOLDER ,
181
+ tree : parser . parse ( fileContent ) ,
182
+ } )
183
+
184
+ const sourcedUris = new Set (
185
+ sourceCommands
186
+ . map ( ( sourceCommand ) => sourceCommand . uri )
187
+ . filter ( ( uri ) => uri !== null ) ,
188
+ )
189
+
190
+ expect ( sourcedUris ) . toEqual (
191
+ new Set ( [
192
+ `file://${ REPO_ROOT_FOLDER } /scripts/release-client.sh` ,
193
+ `file://${ REPO_ROOT_FOLDER } /testing/fixtures/issue206.sh` ,
194
+ `file://${ REPO_ROOT_FOLDER } /testing/fixtures/missing-node.sh` ,
195
+ `file://${ REPO_ROOT_FOLDER } /testing/fixtures/install.sh` ,
196
+ `file://${ REPO_ROOT_FOLDER } /testing/fixtures/issue101.sh` ,
197
+ ] ) ,
198
+ )
199
+
200
+ expect (
201
+ sourceCommands
202
+ . filter ( ( command ) => command . error )
203
+ . map ( ( { error, range } ) => ( {
204
+ error,
205
+ line : range . start . line ,
206
+ } ) ) ,
207
+ ) . toMatchInlineSnapshot ( `
208
+ [
209
+ {
210
+ "error": "failed to resolve path",
211
+ "line": 15,
212
+ },
213
+ ]
214
+ ` )
215
+ } )
152
216
} )
0 commit comments