@@ -13,31 +13,34 @@ const mkdir = promisify(fs.mkdir);
13
13
const readFile = promisify ( fs . readFile ) ;
14
14
const stat = promisify ( fs . stat ) ;
15
15
const readdir = promisify ( fs . readdir ) ;
16
+ const path = require ( 'path' ) ;
17
+
18
+ const scratchDir = path . join ( process . cwd ( ) , 'scratch' ) ;
16
19
17
20
beforeAll ( async ( ) => {
18
- await rimraf ( './scratch' ) ;
21
+ await rimraf ( scratchDir ) ;
19
22
} ) ;
20
23
21
24
beforeEach ( async ( ) => {
22
- await mkdir ( './scratch' ) ;
25
+ await mkdir ( scratchDir ) ;
23
26
} ) ;
24
27
25
28
afterEach ( async ( ) => {
26
- await rimraf ( './scratch' ) ;
29
+ await rimraf ( scratchDir ) ;
27
30
} ) ;
28
31
29
32
describe ( 'createDirectory' , ( ) => {
30
33
test ( 'it creates a new directory with the project name' , async ( ) => {
31
- await createDirectory ( './scratch' , 'test-project' ) ;
32
- const dir = await stat ( './scratch/ test-project') ;
34
+ await createDirectory ( scratchDir , 'test-project' ) ;
35
+ const dir = await stat ( path . join ( scratchDir , ' test-project') ) ;
33
36
expect ( dir . isDirectory ( ) ) ;
34
37
} ) ;
35
38
36
39
test ( 'it throws an error if the directory exists' , async ( ) => {
37
- await mkdir ( './scratch/ test-project') ;
40
+ await mkdir ( path . join ( scratchDir , ' test-project') ) ;
38
41
expect . assertions ( 1 ) ;
39
42
try {
40
- await createDirectory ( './scratch' , 'test-project' ) ;
43
+ await createDirectory ( scratchDir , 'test-project' ) ;
41
44
} catch ( e ) {
42
45
expect ( e . toString ( ) ) . toMatch ( 'EEXIST' ) ;
43
46
}
@@ -46,10 +49,12 @@ describe('createDirectory', () => {
46
49
47
50
describe ( 'createPackageJSON' , ( ) => {
48
51
test ( 'it creates a new package.json file with the name of the project' , async ( ) => {
49
- await createPackageJSON ( './scratch' , 'project-name' ) ;
50
- const file = await stat ( './scratch/ package.json') ;
52
+ await createPackageJSON ( scratchDir , 'project-name' ) ;
53
+ const file = await stat ( path . join ( scratchDir , ' package.json') ) ;
51
54
expect ( file . isFile ( ) ) ;
52
- const packageJSON = JSON . parse ( await readFile ( './scratch/package.json' ) ) ;
55
+ const packageJSON = JSON . parse (
56
+ await readFile ( path . join ( scratchDir , 'package.json' ) )
57
+ ) ;
53
58
expect ( packageJSON . name ) . toEqual ( 'project-name' ) ;
54
59
expect ( packageJSON . engines . node ) . toEqual ( versions . node ) ;
55
60
expect ( packageJSON . devDependencies [ 'twilio-run' ] ) . toEqual (
@@ -58,38 +63,41 @@ describe('createPackageJSON', () => {
58
63
} ) ;
59
64
60
65
test ( 'it rejects if there is already a package.json' , async ( ) => {
61
- fs . closeSync ( fs . openSync ( './scratch/ package.json', 'w' ) ) ;
66
+ fs . closeSync ( fs . openSync ( path . join ( scratchDir , ' package.json') , 'w' ) ) ;
62
67
expect . assertions ( 1 ) ;
63
68
try {
64
- await createPackageJSON ( './scratch' , 'project-name' ) ;
69
+ await createPackageJSON ( scratchDir , 'project-name' ) ;
65
70
} catch ( e ) {
66
71
expect ( e . toString ( ) ) . toMatch ( 'file already exists' ) ;
67
72
}
68
73
} ) ;
69
74
} ) ;
70
75
71
76
describe ( 'createExampleFromTemplates' , ( ) => {
77
+ const templatesDir = path . join ( process . cwd ( ) , 'templates' ) ;
72
78
test ( 'it creates functions and assets directories' , async ( ) => {
73
- await createExampleFromTemplates ( './scratch' ) ;
79
+ await createExampleFromTemplates ( scratchDir ) ;
74
80
75
- const dirs = await readdir ( './scratch' ) ;
76
- const templateDirs = await readdir ( './templates' ) ;
77
- expect ( dirs ) . toEqual ( templateDirs ) ;
81
+ const dirs = await readdir ( scratchDir ) ;
82
+ const templateDirContents = await readdir ( templatesDir ) ;
83
+ expect ( dirs ) . toEqual ( templateDirContents ) ;
78
84
} ) ;
79
85
80
86
test ( 'it copies the functions from the templates/functions directory' , async ( ) => {
81
- await createExampleFromTemplates ( './scratch' ) ;
87
+ await createExampleFromTemplates ( scratchDir ) ;
82
88
83
- const functions = await readdir ( './scratch/functions' ) ;
84
- const templateFunctions = await readdir ( './templates/functions' ) ;
89
+ const functions = await readdir ( path . join ( scratchDir , 'functions' ) ) ;
90
+ const templateFunctions = await readdir (
91
+ path . join ( templatesDir , 'functions' )
92
+ ) ;
85
93
expect ( functions ) . toEqual ( templateFunctions ) ;
86
94
} ) ;
87
95
88
96
test ( 'it rejects if there is already a functions directory' , async ( ) => {
89
- await mkdir ( './scratch/ functions') ;
97
+ await mkdir ( path . join ( scratchDir , ' functions') ) ;
90
98
expect . assertions ( 1 ) ;
91
99
try {
92
- await createExampleFromTemplates ( './scratch' ) ;
100
+ await createExampleFromTemplates ( scratchDir ) ;
93
101
} catch ( e ) {
94
102
expect ( e . toString ( ) ) . toMatch ( 'file already exists' ) ;
95
103
}
@@ -98,22 +106,24 @@ describe('createExampleFromTemplates', () => {
98
106
99
107
describe ( 'createEnvFile' , ( ) => {
100
108
test ( 'it creates a new .env file' , async ( ) => {
101
- await createEnvFile ( './scratch' , {
109
+ await createEnvFile ( scratchDir , {
102
110
accountSid : 'AC123' ,
103
111
authToken : 'qwerty123456'
104
112
} ) ;
105
- const file = await stat ( './scratch/. env') ;
113
+ const file = await stat ( path . join ( scratchDir , '. env') ) ;
106
114
expect ( file . isFile ( ) ) ;
107
- const contents = await readFile ( './scratch/.env' , { encoding : 'utf-8' } ) ;
115
+ const contents = await readFile ( path . join ( scratchDir , '.env' ) , {
116
+ encoding : 'utf-8'
117
+ } ) ;
108
118
expect ( contents ) . toMatch ( 'ACCOUNT_SID=AC123' ) ;
109
119
expect ( contents ) . toMatch ( 'AUTH_TOKEN=qwerty123456' ) ;
110
120
} ) ;
111
121
112
122
test ( 'it rejects if there is already an .env file' , async ( ) => {
113
- fs . closeSync ( fs . openSync ( './scratch/. env', 'w' ) ) ;
123
+ fs . closeSync ( fs . openSync ( path . join ( scratchDir , '. env') , 'w' ) ) ;
114
124
expect . assertions ( 1 ) ;
115
125
try {
116
- await createEnvFile ( './scratch' , {
126
+ await createEnvFile ( scratchDir , {
117
127
accountSid : 'AC123' ,
118
128
authToken : 'qwerty123456'
119
129
} ) ;
@@ -125,18 +135,20 @@ describe('createEnvFile', () => {
125
135
126
136
describe ( 'createNvmrcFile' , ( ) => {
127
137
test ( 'it creates a new .nvmrc file' , async ( ) => {
128
- await createNvmrcFile ( './scratch' ) ;
129
- const file = await stat ( './scratch/. nvmrc') ;
138
+ await createNvmrcFile ( scratchDir ) ;
139
+ const file = await stat ( path . join ( scratchDir , '. nvmrc') ) ;
130
140
expect ( file . isFile ( ) ) ;
131
- const contents = await readFile ( './scratch/.nvmrc' , { encoding : 'utf-8' } ) ;
141
+ const contents = await readFile ( path . join ( scratchDir , '.nvmrc' ) , {
142
+ encoding : 'utf-8'
143
+ } ) ;
132
144
expect ( contents ) . toMatch ( versions . node ) ;
133
145
} ) ;
134
146
135
147
test ( 'it rejects if there is already an .nvmrc file' , async ( ) => {
136
- fs . closeSync ( fs . openSync ( './scratch/. nvmrc', 'w' ) ) ;
148
+ fs . closeSync ( fs . openSync ( path . join ( scratchDir , '. nvmrc') , 'w' ) ) ;
137
149
expect . assertions ( 1 ) ;
138
150
try {
139
- await createNvmrcFile ( './scratch' ) ;
151
+ await createNvmrcFile ( scratchDir ) ;
140
152
} catch ( e ) {
141
153
expect ( e . toString ( ) ) . toMatch ( 'file already exists' ) ;
142
154
}
0 commit comments