@@ -50,67 +50,70 @@ describe("getRangesForDiff", () => {
50
50
51
51
describe ( "getDiffForFile" , ( ) => {
52
52
it ( "should get the staged diff of a file" , ( ) => {
53
- mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( hunks ) ) ;
53
+ mockedChildProcess . execFileSync . mockReturnValueOnce ( Buffer . from ( hunks ) ) ;
54
54
process . env . ESLINT_PLUGIN_DIFF_COMMIT = "1234567" ;
55
55
56
56
const diffFromFile = getDiffForFile ( "./mockfile.js" , true ) ;
57
57
58
- const expectedArguments =
59
- 'git diff --diff-algorithm=histogram --diff-filter=ACM -M100% --relative --staged --unified=0 "1234567"' ;
60
- expect (
61
- mockedChildProcess . execSync . mock . calls [
62
- mockedChildProcess . execSync . mock . calls . length - 1
63
- ] [ 0 ]
64
- . split ( " -- " )
65
- . shift ( )
66
- ) . toEqual ( expectedArguments ) ;
58
+ const expectedCommand = "git" ;
59
+ const expectedArgs =
60
+ 'diff --diff-algorithm=histogram --diff-filter=ACM -M100% --relative --staged --unified=0 "1234567"' ;
61
+
62
+ const lastCall = mockedChildProcess . execFileSync . mock . calls . at ( - 1 ) ;
63
+ const [ command , argsIncludingFile = [ ] ] = lastCall ?? [ "" ] ;
64
+ const args = argsIncludingFile . slice ( 0 , argsIncludingFile . length - 2 ) ;
65
+
66
+ expect ( command ) . toBe ( expectedCommand ) ;
67
+ expect ( args . join ( " " ) ) . toEqual ( expectedArgs ) ;
67
68
expect ( diffFromFile ) . toContain ( "diff --git" ) ;
68
69
expect ( diffFromFile ) . toContain ( "@@" ) ;
69
70
} ) ;
70
71
71
72
it ( "should hit the cached diff of a file" , ( ) => {
72
73
jest . mock ( "child_process" ) . resetAllMocks ( ) ;
73
- mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( hunks ) ) ;
74
+ mockedChildProcess . execFileSync . mockReturnValueOnce ( Buffer . from ( hunks ) ) ;
74
75
75
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 0 ) ;
76
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 0 ) ;
76
77
const diffFromFileA = getDiffForFile ( "./mockfileCache.js" ) ;
77
78
const diffFromFileB = getDiffForFile ( "./mockfileCache.js" ) ;
78
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 1 ) ;
79
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
79
80
expect ( diffFromFileA ) . toEqual ( diffFromFileB ) ;
80
81
81
- mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( hunks ) ) ;
82
+ mockedChildProcess . execFileSync . mockReturnValueOnce ( Buffer . from ( hunks ) ) ;
82
83
getDiffForFile ( "./mockfileMiss.js" ) ;
83
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 2 ) ;
84
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
84
85
} ) ;
85
86
} ) ;
86
87
87
88
describe ( "hasCleanIndex" , ( ) => {
88
89
it ( "returns false instead of throwing" , ( ) => {
89
90
jest . mock ( "child_process" ) . resetAllMocks ( ) ;
90
- mockedChildProcess . execSync . mockImplementationOnce ( ( ) => {
91
+ mockedChildProcess . execFileSync . mockImplementationOnce ( ( ) => {
91
92
throw Error ( "mocked error" ) ;
92
93
} ) ;
93
94
expect ( hasCleanIndex ( "" ) ) . toEqual ( false ) ;
94
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalled ( ) ;
95
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalled ( ) ;
95
96
} ) ;
96
97
97
98
it ( "returns true otherwise" , ( ) => {
98
99
jest . mock ( "child_process" ) . resetAllMocks ( ) ;
99
- mockedChildProcess . execSync . mockReturnValue ( Buffer . from ( "" ) ) ;
100
+ mockedChildProcess . execFileSync . mockReturnValue ( Buffer . from ( "" ) ) ;
100
101
expect ( hasCleanIndex ( "" ) ) . toEqual ( true ) ;
101
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalled ( ) ;
102
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalled ( ) ;
102
103
} ) ;
103
104
} ) ;
104
105
105
106
describe ( "getDiffFileList" , ( ) => {
106
107
it ( "should get the list of staged files" , ( ) => {
107
108
jest . mock ( "child_process" ) . resetAllMocks ( ) ;
108
- mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( diffFileList ) ) ;
109
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 0 ) ;
109
+ mockedChildProcess . execFileSync . mockReturnValueOnce (
110
+ Buffer . from ( diffFileList )
111
+ ) ;
112
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 0 ) ;
110
113
const fileListA = getDiffFileList ( ) ;
111
114
const fileListB = getDiffFileList ( ) ;
112
115
113
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 1 ) ;
116
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
114
117
expect ( fileListA ) . toEqual (
115
118
[ "file1" , "file2" , "file3" ] . map ( ( p ) => path . resolve ( p ) )
116
119
) ;
@@ -121,13 +124,15 @@ describe("getDiffFileList", () => {
121
124
describe ( "getUntrackedFileList" , ( ) => {
122
125
it ( "should get the list of untracked files" , ( ) => {
123
126
jest . mock ( "child_process" ) . resetAllMocks ( ) ;
124
- mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( diffFileList ) ) ;
125
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 0 ) ;
127
+ mockedChildProcess . execFileSync . mockReturnValueOnce (
128
+ Buffer . from ( diffFileList )
129
+ ) ;
130
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 0 ) ;
126
131
const fileListA = getUntrackedFileList ( ) ;
127
132
const staged = false ;
128
133
const fileListB = getUntrackedFileList ( staged ) ;
129
134
130
- expect ( mockedChildProcess . execSync ) . toHaveBeenCalledTimes ( 1 ) ;
135
+ expect ( mockedChildProcess . execFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
131
136
expect ( fileListA ) . toEqual (
132
137
[ "file1" , "file2" , "file3" ] . map ( ( p ) => path . resolve ( p ) )
133
138
) ;
@@ -142,7 +147,9 @@ describe("getUntrackedFileList", () => {
142
147
143
148
describe ( "getGitFileList" , ( ) => {
144
149
it ( "should get the list of committed files" , ( ) => {
145
- mockedChildProcess . execSync . mockReturnValueOnce ( Buffer . from ( diffFileList ) ) ;
150
+ mockedChildProcess . execFileSync . mockReturnValueOnce (
151
+ Buffer . from ( diffFileList )
152
+ ) ;
146
153
expect ( getGitFileList ( ) ) . toEqual (
147
154
[ "file1" , "file2" , "file3" ] . map ( ( p ) => path . resolve ( p ) )
148
155
) ;
0 commit comments