1
1
import * as tmp from "tmp"
2
- import * as fs from "fs-extra"
3
2
import * as path from "path"
4
3
5
4
import { spawnSafeSync } from "../src/spawnSafe"
@@ -9,32 +8,51 @@ import { parsePatch } from "../src/patch/parse"
9
8
10
9
import { TestCase , Files } from "./testCases"
11
10
11
+ jest . mock ( "fs-extra" , ( ) => {
12
+ let workingFiles : Files
13
+
14
+ function setWorkingFiles ( files : Files ) {
15
+ workingFiles = files
16
+ }
17
+
18
+ function getWorkingFiles ( ) {
19
+ return workingFiles
20
+ }
21
+
22
+ return {
23
+ setWorkingFiles,
24
+ getWorkingFiles,
25
+ ensureDirSync : jest . fn ( ) ,
26
+ readFileSync : jest . fn ( path => getWorkingFiles ( ) [ path ] . contents ) ,
27
+ writeFileSync : jest . fn (
28
+ ( path : string , contents : string , opts ?: { mode ?: number } ) => {
29
+ getWorkingFiles ( ) [ path ] = {
30
+ contents,
31
+ mode : opts && typeof opts . mode === "number" ? opts . mode : 0o644 ,
32
+ }
33
+ } ,
34
+ ) ,
35
+ unlinkSync : jest . fn ( path => delete getWorkingFiles ( ) [ path ] ) ,
36
+ moveSync : jest . fn ( ( from , to ) => {
37
+ getWorkingFiles ( ) [ to ] = getWorkingFiles ( ) [ from ]
38
+ delete getWorkingFiles ( ) [ from ]
39
+ } ) ,
40
+ }
41
+ } )
42
+
12
43
function writeFiles ( cwd : string , files : Files ) : void {
44
+ const mkdirpSync = require ( "fs-extra/lib/mkdirs/index.js" ) . mkdirpSync
45
+ const writeFileSync = require ( "fs" ) . writeFileSync
13
46
Object . keys ( files ) . forEach ( filePath => {
14
47
if ( ! filePath . startsWith ( ".git/" ) ) {
15
- fs . mkdirpSync ( path . join ( cwd , path . dirname ( filePath ) ) )
16
- fs . writeFileSync ( path . join ( cwd , filePath ) , files [ filePath ] . contents , {
48
+ mkdirpSync ( path . join ( cwd , path . dirname ( filePath ) ) )
49
+ writeFileSync ( path . join ( cwd , filePath ) , files [ filePath ] . contents , {
17
50
mode : files [ filePath ] . mode ,
18
51
} )
19
52
}
20
53
} )
21
54
}
22
55
23
- let workingFiles : Files
24
-
25
- function setWorkingFiles ( files : Files ) {
26
- workingFiles = files
27
- }
28
-
29
- function getWorkingFiles ( ) {
30
- return workingFiles
31
- }
32
-
33
- const properReadFileSync = fs . readFileSync
34
- const properWriteFileSync = fs . writeFileSync
35
- const properUnlinkSync = fs . unlinkSync
36
- const properMoveSync = fs . moveSync
37
-
38
56
function removeLeadingSpaceOnBlankLines ( patchFileContents : string ) : string {
39
57
return patchFileContents
40
58
. split ( "\n" )
@@ -43,104 +61,78 @@ function removeLeadingSpaceOnBlankLines(patchFileContents: string): string {
43
61
}
44
62
45
63
export function executeTestCase ( testCase : TestCase ) {
64
+ const fs = require ( "fs-extra" )
65
+
46
66
function reportingFailures ( f : ( ) => void ) : void {
47
67
try {
48
68
f ( )
49
69
} catch ( e ) {
50
70
console . error ( "TEST CASE FAILED" , {
51
71
testCase,
52
- workingFiles : getWorkingFiles ( ) ,
72
+ workingFiles : fs . getWorkingFiles ( ) ,
53
73
} )
54
74
throw e
55
75
}
56
76
}
57
77
58
- describe ( "the test case" , ( ) => {
59
- beforeEach ( ( ) => {
60
- ; ( fs as any ) . readFileSync = jest . fn (
61
- path => getWorkingFiles ( ) [ path ] . contents ,
62
- )
63
- ; ( fs as any ) . writeFileSync = jest . fn (
64
- ( path : string , contents : string , opts ?: { mode ?: number } ) => {
65
- getWorkingFiles ( ) [ path ] = {
66
- contents,
67
- mode : opts && typeof opts . mode === "number" ? opts . mode : 0o644 ,
68
- }
69
- } ,
70
- )
71
- ; ( fs as any ) . unlinkSync = jest . fn ( path => delete getWorkingFiles ( ) [ path ] )
72
- ; ( fs as any ) . moveSync = jest . fn ( ( from , to ) => {
73
- getWorkingFiles ( ) [ to ] = getWorkingFiles ( ) [ from ]
74
- delete getWorkingFiles ( ) [ from ]
75
- } )
76
- } )
78
+ const tmpDir = tmp . dirSync ( { unsafeCleanup : true , mode : 0o100777 } )
77
79
78
- afterEach ( ( ) => {
79
- ; ( fs as any ) . readFileSync = properReadFileSync
80
- ; ( fs as any ) . writeFileSync = properWriteFileSync
81
- ; ( fs as any ) . unlinkSync = properUnlinkSync
82
- ; ( fs as any ) . moveSync = properMoveSync
83
- } )
80
+ spawnSafeSync ( "git" , [ "init" ] , { cwd : tmpDir . name } )
84
81
85
- const tmpDir = tmp . dirSync ( { unsafeCleanup : true , mode : 0o100777 } )
82
+ writeFiles ( tmpDir . name , testCase . cleanFiles )
86
83
87
- spawnSafeSync ( "git" , [ "init" ] , { cwd : tmpDir . name } )
84
+ spawnSafeSync ( "git" , [ "add" , "-A" ] , { cwd : tmpDir . name } )
85
+ spawnSafeSync ( "git" , [ "commit" , "-m" , "blah" ] , {
86
+ cwd : tmpDir . name ,
87
+ } )
88
+ spawnSafeSync ( "git" , [ "rm" , "-rf" , "*" ] , {
89
+ cwd : tmpDir . name ,
90
+ } )
88
91
89
- writeFiles ( tmpDir . name , testCase . cleanFiles )
92
+ writeFiles ( tmpDir . name , testCase . modifiedFiles )
93
+ spawnSafeSync ( "git" , [ "add" , "-A" ] , { cwd : tmpDir . name } )
90
94
91
- spawnSafeSync ( "git" , [ "add" , "-A" ] , { cwd : tmpDir . name } )
92
- spawnSafeSync ( "git" , [ "commit" , "-m" , "blah" ] , {
95
+ const patchResult = spawnSafeSync (
96
+ "git" ,
97
+ [ "diff" , "--color=never" , "--cached" ] ,
98
+ {
93
99
cwd : tmpDir . name ,
94
- } )
95
- spawnSafeSync ( "git" , [ "rm" , "-rf" , "*" ] , {
96
- cwd : tmpDir . name ,
97
- } )
100
+ logStdErrOnError : true ,
101
+ throwOnError : true ,
102
+ } ,
103
+ )
98
104
99
- writeFiles ( tmpDir . name , testCase . modifiedFiles )
100
- spawnSafeSync ( "git" , [ "add" , "-A" ] , { cwd : tmpDir . name } )
105
+ const patchFileContents = patchResult . stdout . toString ( )
101
106
102
- const patchResult = spawnSafeSync (
103
- "git" ,
104
- [ "diff" , "--color=never" , "--cached" ] ,
105
- {
106
- cwd : tmpDir . name ,
107
- logStdErrOnError : true ,
108
- throwOnError : true ,
109
- } ,
110
- )
107
+ const patchFileContentsWithBlankLines = removeLeadingSpaceOnBlankLines (
108
+ patchFileContents ,
109
+ )
111
110
112
- const patchFileContents = patchResult . stdout . toString ( )
113
-
114
- const patchFileContentsWithBlankLines = removeLeadingSpaceOnBlankLines (
115
- patchFileContents ,
116
- )
117
-
118
- it ( "looks the same whether parsed with blank lines or not" , ( ) => {
119
- reportingFailures ( ( ) => {
120
- expect ( parsePatch ( patchFileContents ) ) . toEqual (
121
- parsePatch ( patchFileContentsWithBlankLines ) ,
122
- )
123
- } )
111
+ it ( "looks the same whether parsed with blank lines or not" , ( ) => {
112
+ reportingFailures ( ( ) => {
113
+ expect ( parsePatch ( patchFileContents ) ) . toEqual (
114
+ parsePatch ( patchFileContentsWithBlankLines ) ,
115
+ )
124
116
} )
117
+ } )
125
118
126
- // console.log(patchFileContents)
119
+ // console.log(patchFileContents)
127
120
128
- it ( "works forwards" , ( ) => {
129
- setWorkingFiles ( { ...testCase . cleanFiles } )
130
- reportingFailures ( ( ) => {
131
- const effects = patch ( patchFileContents )
132
- executeEffects ( effects )
133
- expect ( getWorkingFiles ( ) ) . toEqual ( testCase . modifiedFiles )
134
- } )
121
+ it ( "works forwards" , ( ) => {
122
+ fs . setWorkingFiles ( { ...testCase . cleanFiles } )
123
+ reportingFailures ( ( ) => {
124
+ const effects = patch ( patchFileContents )
125
+ executeEffects ( effects )
126
+ expect ( fs . getWorkingFiles ( ) ) . toEqual ( testCase . modifiedFiles )
135
127
} )
128
+ } )
136
129
137
- it ( "works backwards" , ( ) => {
138
- setWorkingFiles ( { ...testCase . modifiedFiles } )
139
- reportingFailures ( ( ) => {
140
- const result = patch ( patchFileContents , { reverse : true } )
141
- executeEffects ( result )
142
- expect ( getWorkingFiles ( ) ) . toEqual ( testCase . cleanFiles )
143
- } )
130
+ it ( "works backwards" , ( ) => {
131
+ fs . setWorkingFiles ( { ...testCase . modifiedFiles } )
132
+ reportingFailures ( ( ) => {
133
+ const result = patch ( patchFileContents , { reverse : true } )
134
+ executeEffects ( result )
135
+ expect ( fs . getWorkingFiles ( ) ) . toEqual ( testCase . cleanFiles )
144
136
} )
145
137
} )
146
138
}
0 commit comments