-
Notifications
You must be signed in to change notification settings - Fork 430
/
test.ts
298 lines (274 loc) Β· 12.4 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import fs from 'fs-extra';
import {after, before, describe, it} from 'mocha';
import path, {dirname} from 'path';
import {readPackageUpSync} from 'read-pkg-up';
import {fileURLToPath} from 'url';
import {getAppsScriptFileName, getLocalFileType} from '../src/files.js';
import {ERROR, LOG} from '../src/messages.js';
import {extractScriptId, URL} from '../src/urls.js';
import {getApiFileType, getDefaultProjectName, getWebApplicationURL, saveProject} from '../src/utils.js';
import {CLASP, CLASP_PATHS, CLASP_USAGE, IS_PR, SCRIPT_ID} from './constants.js';
import {backupSettings, cleanup, restoreSettings, setup} from './functions.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const manifest = readPackageUpSync({cwd: __dirname});
describe.skip('Test --help for each function', () => {
const expectHelp = (command: string, expected: string) => {
const result = spawnSync(CLASP, [command, '--help'], {encoding: 'utf8'});
expect(result.status).to.equal(0);
expect(result.stdout).to.include(expected);
};
it('should run --help', () => expectHelp('run', 'Run a function in your Apps Scripts project'));
it('should logs --help', () => expectHelp('logs', 'Shows the StackDriver logs'));
it('should login --help', () => expectHelp('login', 'Log in to script.google.com'));
it('should logout --help', () => expectHelp('logout', 'Log out'));
it('should create --help', () => expectHelp('create', 'Create a script'));
it('should clone --help', () => expectHelp('clone', 'Clone a project'));
it('should pull --help', () => expectHelp('pull', 'Fetch a remote project'));
it('should push --help', () => expectHelp('push', 'Update the remote project'));
it('should status --help', () => expectHelp('status', 'Lists files that will be pushed by clasp'));
it('should open --help', () => expectHelp('open', 'Open a script'));
it('should deployments --help', () => expectHelp('deployments', 'List deployment ids of a script'));
it('should undeploy --help', () => expectHelp('undeploy', 'Undeploy a deployment of a project'));
it('should versions --help', () => expectHelp('versions', 'List versions of a script'));
it('should version --help', () => expectHelp('version', 'Creates an immutable version of the script'));
it('should list --help', () => expectHelp('list', 'List App Scripts projects'));
it('should apis --help', () => expectHelp('apis', 'List, enable, or disable APIs'));
it('should help --help', () => expectHelp('help', 'Display help'));
});
describe('Test extractScriptId function', () => {
it('should return scriptId correctly', () => {
expect(extractScriptId(SCRIPT_ID)).to.equal(SCRIPT_ID);
expect(extractScriptId(URL.SCRIPT(SCRIPT_ID))).to.equal(SCRIPT_ID);
});
});
describe('Test clasp pull function', () => {
before(function () {
if (IS_PR) {
this.skip();
}
setup();
});
it('should pull an existing project correctly', () => {
const result = spawnSync(CLASP, ['pull'], {encoding: 'utf8'});
expect(result.stdout).to.contain('Cloned');
expect(result.stdout).to.contain('files.');
expect(result.status).to.equal(0);
});
after(cleanup);
});
describe('Test URL utils function', () => {
it('should create Script URL correctly', () => {
const expectedUrl = `https://script.google.com/d/${SCRIPT_ID}/edit`;
expect(URL.SCRIPT(SCRIPT_ID)).to.equal(expectedUrl);
});
it('should create Creds URL correctly', () => {
const expectedURL = `https://console.developers.google.com/apis/credentials?project=${SCRIPT_ID}`;
expect(URL.CREDS(SCRIPT_ID)).to.equal(expectedURL);
});
});
describe('Test clasp version and versions function', () => {
before(function () {
if (IS_PR) {
this.skip();
}
setup();
});
let versionNumber = 0;
it('should prompt for version description', () => {
const result = spawnSync(CLASP, ['version'], {encoding: 'utf8'});
expect(result.stderr).to.equal('');
expect(result.stdout).to.contain(LOG.GIVE_DESCRIPTION);
expect(result.status).to.equal(0);
});
it('should create a new version correctly', () => {
const result = spawnSync(CLASP, ['version', 'xxx'], {encoding: 'utf8'});
versionNumber = Number(result.stdout.slice(result.stdout.lastIndexOf(' '), -2));
expect(versionNumber).to.be.greaterThan(0);
});
// TODO: this test needs to be updated
it.skip('should list versions correctly', () => {
const result = spawnSync(CLASP, ['versions'], {encoding: 'utf8'});
expect(result.stdout).to.contain('Versions');
if (versionNumber) expect(result.stdout).to.contain(`${versionNumber} - `);
expect(result.status).to.equal(0);
});
after(cleanup);
});
describe('Test setting function', () => {
before(function () {
if (IS_PR) {
this.skip();
}
setup();
});
it('should return current setting value', () => {
const result = spawnSync(CLASP, ['setting', 'scriptId'], {encoding: 'utf8'});
expect(result.stdout).to.equal(process.env.SCRIPT_ID);
});
it('should update .clasp.json with provided value', () => {
const result = spawnSync(CLASP, ['setting', 'scriptId', 'test'], {encoding: 'utf8'});
const fileContents = fs.readFileSync('.clasp.json', 'utf8');
expect(result.stdout).to.contain('Updated "scriptId":');
expect(result.stdout).to.contain('β "test"');
expect(fileContents).to.contain('"test"');
});
it('should error on unknown keys', () => {
// Test getting
let result = spawnSync(CLASP, ['setting', 'foo'], {encoding: 'utf8'});
expect(result.status).to.equal(1);
expect(result.stderr).to.contain(ERROR.UNKNOWN_KEY('foo'));
// Test setting
result = spawnSync(CLASP, ['setting', 'bar', 'foo'], {encoding: 'utf8'});
expect(result.status).to.equal(1);
expect(result.stderr).to.contain(ERROR.UNKNOWN_KEY('bar'));
});
after(cleanup);
});
describe('Test getAppsScriptFileName function from files', () => {
it('should return the basename correctly', () => {
expect(getAppsScriptFileName('./', 'appsscript.json')).to.equal('appsscript');
expect(getAppsScriptFileName('', 'appsscript.json')).to.equal('appsscript');
expect(getAppsScriptFileName('./dist', './dist/appsscript.json')).to.equal('appsscript');
expect(getAppsScriptFileName('./dist', './dist/foo/Code.js')).to.equal('foo/Code');
});
});
describe('Test URL helper from utils', () => {
it('should return the scriptURL correctly', () => {
const url = URL.SCRIPT('abcdefghijklmnopqrstuvwxyz');
expect(url).to.equal('https://script.google.com/d/abcdefghijklmnopqrstuvwxyz/edit');
});
});
describe('Test getWebApplicationURL function from utils', () => {
it('should return the scriptURL correctly', () => {
const url = getWebApplicationURL({
entryPoints: [
{
entryPointType: 'WEB_APP',
webApp: {
url: 'https://script.google.com/macros/s/abcdefghijklmnopqrstuvwxyz/exec',
},
},
],
});
expect(url).to.equal('https://script.google.com/macros/s/abcdefghijklmnopqrstuvwxyz/exec');
});
});
describe('Test getDefaultProjectName function from utils', () => {
it('should return the current directory name correctly', () => {
expect(getDefaultProjectName()).to.equal('Clasp');
});
});
describe('Test getLocalFileType function from utils', () => {
it('should return the lowercase file type correctly', () => {
expect(getLocalFileType('SERVER_JS')).to.equal('js');
expect(getLocalFileType('GS')).to.equal('gs');
expect(getLocalFileType('JS')).to.equal('js');
expect(getLocalFileType('HTML')).to.equal('html');
});
it('should return the specified file extension if the file type is SERVER_JS', () => {
expect(getLocalFileType('SERVER_JS', 'gs')).to.equal('gs');
expect(getLocalFileType('GS', 'js')).to.equal('gs');
expect(getLocalFileType('JS', 'gs')).to.equal('js');
expect(getLocalFileType('HTML', 'js')).to.equal('html');
});
});
describe('Test getAPIFileType function from utils', () => {
it('should return the uppercase file type correctly', () => {
expect(getApiFileType('file.GS')).to.equal('SERVER_JS');
expect(getApiFileType('file.JS')).to.equal('SERVER_JS');
expect(getApiFileType('file.js')).to.equal('SERVER_JS');
expect(getApiFileType('file.jsx')).to.equal('JSX');
expect(getApiFileType('file.js.html')).to.equal('HTML');
});
});
describe('Test saveProject function from utils', () => {
it('should save the scriptId correctly', () => {
spawnSync('rm', ['.clasp.json']);
const isSaved = async () => {
await saveProject({scriptId: '12345'});
const id = fs.readFileSync(path.join(__dirname, '/../.clasp.json'), 'utf8');
expect(id).to.equal('{"scriptId":"12345"}');
};
expect(isSaved).to.not.equal(null);
});
it('should save the scriptId, rootDir correctly', () => {
spawnSync('rm', ['.clasp.json']);
const isSaved = async () => {
await saveProject({scriptId: '12345', rootDir: './dist'});
const id = fs.readFileSync(path.join(__dirname, '/../.clasp.json'), 'utf8');
expect(id).to.equal('{"scriptId":"12345","rootDir":"./dist"}');
};
expect(isSaved).to.not.equal(null);
});
});
describe('Test variations of clasp help', () => {
const expectHelp = (variation: string) => {
const result = spawnSync(CLASP, [variation], {encoding: 'utf8'});
expect(result.status).to.equal(0);
expect(result.stdout).to.include(CLASP_USAGE);
};
it('should show help for clasp help', () => expectHelp('help'));
it('should show help for clasp --help', () => expectHelp('--help'));
it('should show help for clasp -h', () => expectHelp('-h'));
});
describe('Test variations of clasp --version', () => {
const expectVersion = (variation: string) => {
const result = spawnSync(CLASP, [variation], {encoding: 'utf8'});
expect(result.status).to.equal(0);
expect(result.stdout).to.include(manifest ? manifest.packageJson.version : 'unknown');
};
it('should show version for clasp --version', () => expectVersion('--version'));
it('should show version for clasp -v', () => expectVersion('-v'));
});
describe('Test unknown functions', () => {
it('should show version correctly', () => {
const result = spawnSync(CLASP, ['unknown'], {encoding: 'utf8'});
expect(result.stderr).to.contain('Unknown command');
expect(result.status).to.equal(1);
});
});
describe('Test all functions while logged out', () => {
before(() => {
backupSettings();
if (fs.existsSync(CLASP_PATHS.rcGlobal)) fs.removeSync(CLASP_PATHS.rcGlobal);
if (fs.existsSync(CLASP_PATHS.rcLocal)) fs.removeSync(CLASP_PATHS.rcLocal);
});
after(restoreSettings);
const expectNoCredentials = (command: string) => {
const result = spawnSync(CLASP, [command], {encoding: 'utf8'});
expect(result.status).to.equal(1);
// expect(result.stderr).to.include(ERROR.NO_CREDENTIALS);
};
it('should fail to list (no credentials)', () => expectNoCredentials('list'));
it('should fail to clone (no credentials)', () => expectNoCredentials('clone'));
it('should fail to push (no credentials)', () => expectNoCredentials('push'));
it('should fail to deployments (no credentials)', () => expectNoCredentials('deployments'));
it('should fail to deploy (no credentials)', () => expectNoCredentials('deploy'));
it('should fail to version (no credentials)', () => expectNoCredentials('version'));
it('should fail to versions (no credentials)', () => expectNoCredentials('versions'));
// TODO: all test should have same order of checks
// and should all return ERROR.NO_CREDENTIALS
it('should fail to pull (no .clasp.json file)', () => {
const result = spawnSync(CLASP, ['pull'], {encoding: 'utf8'});
expect(result.status).to.equal(1);
// Should be ERROR.NO_CREDENTIALS
// see: https://github.com/google/clasp/issues/278
expect(result.stderr).to.contain(ERROR.SETTINGS_DNE());
});
it('should fail to open (no .clasp.json file)', () => {
const result = spawnSync(CLASP, ['open'], {encoding: 'utf8'});
expect(result.status).to.equal(1);
// Should be ERROR.NO_CREDENTIALS
// see: https://github.com/google/clasp/issues/278
expect(result.stderr).to.contain(ERROR.SETTINGS_DNE());
});
it('should fail to show logs (no .clasp.json file)', () => {
const result = spawnSync(CLASP, ['logs'], {encoding: 'utf8'});
expect(result.status).to.equal(1);
// Should be ERROR.NO_CREDENTIALS
// see: https://github.com/google/clasp/issues/278
expect(result.stderr).to.contain(ERROR.SETTINGS_DNE());
});
});