Skip to content

Commit

Permalink
chore: Re-enable test/commands (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Bazyl committed Jan 8, 2025
1 parent 437bf6c commit 1c75e06
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 160 deletions.
1 change: 0 additions & 1 deletion src/commands/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export default async (options: CommandOption): Promise<void> => {
}
},
undefined: async () => {
await apiSubCommands.list();
console.log(`# Try these commands:
- clasp apis list
- clasp apis enable slides
Expand Down
3 changes: 2 additions & 1 deletion src/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export default async (

spinner.start(LOG.CLONING);

const files = await fetchProject(id, versionNumber);
await saveProject({scriptId: id, rootDir: config.projectRootDirectory}, false);
await writeProjectFiles(await fetchProject(id, versionNumber), config.projectRootDirectory);
await writeProjectFiles(files, config.projectRootDirectory);
await status();
};

Expand Down
23 changes: 11 additions & 12 deletions test/commands/apis.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import {after, before, describe, it} from 'mocha';

import {URL} from '../../src/urls.js';
import {CLASP, PROJECT_ID} from '../constants.js';
import {cleanup, setup} from '../functions.js';
import {PROJECT_ID} from '../constants.js';
import {cleanup, runClasp, setup} from '../functions.js';

describe('Test clasp apis functions', () => {
before(setup);
it('should list apis correctly', () => {
const result = spawnSync(CLASP, ['apis', 'list'], {encoding: 'utf8'});
const result = runClasp(['apis', 'list']);
expect(result.stdout).to.contain('# Currently enabled APIs:');
expect(result.stdout).to.contain('# List of available APIs:');
expect(result.stderr).to.equal('');
expect(result.status).to.equal(0);
});
it('should ask for an API when trying to enable', () => {
const result = spawnSync(CLASP, ['apis', 'enable'], {encoding: 'utf8'});
const result = runClasp(['apis', 'enable']);
expect(result.stderr).to.contain('An API name is required.');
expect(result.status).to.equal(1);
});
it('should enable sheets', () => {
const result = spawnSync(CLASP, ['apis', 'enable', 'sheets'], {encoding: 'utf8'});
const result = runClasp(['apis', 'enable', 'sheets']);
expect(result.stdout).to.contain('Enabled sheets API.');
expect(result.stderr).to.equal('');
expect(result.status).to.equal(0);
});
it('should give error message for non-existent API', () => {
const result = spawnSync(CLASP, ['apis', 'enable', 'fakeApi'], {encoding: 'utf8'});
const result = runClasp(['apis', 'enable', 'fakeApi']);
expect(result.stderr).to.contain("API fakeApi doesn't exist. Try 'clasp apis enable sheets'.");
expect(result.status).to.equal(1);
});
it('should ask for an API when trying to disable', () => {
const result = spawnSync(CLASP, ['apis', 'disable'], {encoding: 'utf8'});
const result = runClasp(['apis', 'disable']);
expect(result.stderr).to.contain('An API name is required.');
expect(result.status).to.equal(1);
});
it('should disable apis correctly', () => {
const result = spawnSync(CLASP, ['apis', 'disable', 'sheets'], {encoding: 'utf8'});
const result = runClasp(['apis', 'disable', 'sheets']);
expect(result.stdout).to.contain('Disabled sheets API.');
expect(result.stderr).to.equal('');
expect(result.status).to.equal(0);
});
it('should show suggestions for using clasp apis', () => {
const result = spawnSync(CLASP, ['apis'], {encoding: 'utf8'});
const result = runClasp(['apis']);
expect(result.stdout).to.contain(`# Try these commands:
- clasp apis list
- clasp apis enable slides
Expand All @@ -52,12 +51,12 @@ describe('Test clasp apis functions', () => {
expect(result.status).to.equal(0);
});
it('should error with unknown subcommand', () => {
const result = spawnSync(CLASP, ['apis', 'unknown'], {encoding: 'utf8'});
const result = runClasp(['apis', 'unknown']);
expect(result.stderr).to.contain('Unknown command');
expect(result.status).to.equal(1);
});
it('should open APIs dashboard', () => {
const result = spawnSync(CLASP, ['apis', '--open'], {encoding: 'utf8'});
const result = runClasp(['apis', '--open']);
expect(result.stdout).to.contain(URL.APIS(PROJECT_ID));
expect(result.stderr).to.equal('');
expect(result.status).to.equal(0);
Expand Down
24 changes: 15 additions & 9 deletions test/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {after, before, describe, it} from 'mocha';

import {URL} from '../../src/urls.js';
import {ERROR, LOG} from '../../src/messages.js';
import {CLASP, SCRIPT_ID} from '../constants.js';
import {cleanup, setup} from '../functions.js';
import {SCRIPT_ID} from '../constants.js';
import {cleanup, runClasp, setup} from '../functions.js';

describe('Test clasp clone <scriptId> function', () => {
before(setup);
it('should clone a project with scriptId correctly', () => {
cleanup();
const result = spawnSync(CLASP, ['clone', SCRIPT_ID], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
const result = runClasp(['clone', SCRIPT_ID], {maxBuffer: 10 * 1024 * 1024});
expect(result.stdout).to.contain('Cloned');
expect(result.stdout).to.contain('files.');
expect(result.stdout).to.contain(LOG.STATUS_PUSH);
Expand All @@ -21,7 +21,7 @@ describe('Test clasp clone <scriptId> function', () => {
});
it('should clone a project with scriptURL correctly', () => {
cleanup();
const result = spawnSync(CLASP, ['clone', URL.SCRIPT(SCRIPT_ID)], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
const result = runClasp(['clone', URL.SCRIPT(SCRIPT_ID)], {maxBuffer: 10 * 1024 * 1024});
expect(result.stdout).to.contain('Cloned');
expect(result.stdout).to.contain('files.');
expect(result.stdout).to.contain(LOG.STATUS_PUSH);
Expand All @@ -30,24 +30,30 @@ describe('Test clasp clone <scriptId> function', () => {
});
it('should give an error on a non-existing project', () => {
fs.removeSync('./.clasp.json');
const result = spawnSync(CLASP, ['clone', 'non-existing-project'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
const result = runClasp(['clone', 'non-existing-project'], {maxBuffer: 10 * 1024 * 1024});
expect(result.stderr).to.contain(ERROR.SCRIPT_ID);
expect(result.status).to.equal(1);
});
it('should not write clasp config for non-existing project', () => {
fs.removeSync('./.clasp.json');
const result = runClasp(['clone', 'non-existing-project'], {maxBuffer: 10 * 1024 * 1024});
expect(result.status).to.equal(1);
expect(fs.existsSync('./.clasp.json')).to.be.false;
});
after(cleanup);
});

describe('Test clasp clone function', () => {
before(setup);
it('should prompt for which script to clone correctly', () => {
spawnSync('rm', ['.clasp.json']);
const result = spawnSync(CLASP, ['clone'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
const result = runClasp(['clone'], {maxBuffer: 10 * 1024 * 1024});
expect(result.stdout).to.contain(LOG.CLONE_SCRIPT_QUESTION);
expect(result.status).to.equal(0);
});
it('should prompt which project to clone and clone it', () => {
cleanup();
const result = spawnSync(CLASP, ['clone'], {encoding: 'utf8', input: '\n', maxBuffer: 10 * 1024 * 1024});
const result = runClasp(['clone'], {input: '\n', maxBuffer: 10 * 1024 * 1024});
expect(result.stdout).to.contain(LOG.CLONE_SCRIPT_QUESTION);
expect(result.stdout).to.contain('Cloned');
expect(result.stdout).to.contain('files.');
Expand All @@ -57,8 +63,8 @@ describe('Test clasp clone function', () => {
});
it('should give an error if .clasp.json already exists', () => {
fs.writeFileSync('.clasp.json', '');
const result = spawnSync(CLASP, ['clone'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
expect(result.stderr).to.contain('Project file (.clasp.json) already exists.');
const result = runClasp(['clone'], {maxBuffer: 10 * 1024 * 1024});
expect(result.stderr).to.match(/Project file \(.*\) already exists./);
expect(result.status).to.equal(1);
});
after(cleanup);
Expand Down
24 changes: 9 additions & 15 deletions test/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import fs from 'fs-extra';
import {after, before, describe, it} from 'mocha';

import {LOG} from '../../src/messages.js';
import {CLASP} from '../constants.js';
import {cleanup, setup} from '../functions.js';
import {cleanup, runClasp, setup} from '../functions.js';

describe('Test clasp create function', () => {
before(setup);
it('should prompt for a project name correctly', () => {
spawnSync('rm', ['.clasp.json']);
const result = spawnSync(CLASP, ['create'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
fs.removeSync('.clasp.json');
const result = runClasp(['create'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
expect(result.stdout).to.contain(LOG.CREATE_SCRIPT_QUESTION);
expect(result.status).to.equal(0);
});
it('should not prompt for project name', () => {
fs.writeFileSync('.clasp.json', '');
const result = spawnSync(CLASP, ['create'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
expect(result.stderr).to.contain('Project file (.clasp.json) already exists.');
const result = runClasp(['create'], {encoding: 'utf8', maxBuffer: 10 * 1024 * 1024});
expect(result.stderr).to.match(/Project file \(.*\) already exists./);
});
after(cleanup);
});

describe('Test clasp create <title> function', () => {
before(setup);
it('should create a new project named <title> correctly', () => {
spawnSync('rm', ['.clasp.json']);
const result = spawnSync(CLASP, ['create', '--type', 'Standalone', '--title', 'myTitle'], {
encoding: 'utf8',
});
fs.removeSync('.clasp.json');
const result = runClasp(['create', '--type', 'Standalone', '--title', 'myTitle']);
expect(result.stdout).to.contain('Created new Standalone script: https://script.google.com/d/');
expect(result.status).to.equal(0);
});
Expand All @@ -38,10 +34,8 @@ describe('Test clasp create <title> function', () => {
describe('Test clasp create <parentId> function', () => {
before(setup);
it('should not prompt for script types with parentId', () => {
spawnSync('rm', ['.clasp.json']);
const result = spawnSync(CLASP, ['create', '--parentId', '"1D_Gxyv*****************************NXO7o"'], {
encoding: 'utf8',
});
fs.removeSync('.clasp.json');
const result = runClasp(['create', '--parentId', '"1D_Gxyv*****************************NXO7o"']);
expect(result.stdout).to.not.contain(LOG.CREATE_SCRIPT_QUESTION);
expect(result.stderr).to.contain('Request contains an invalid argument.');
});
Expand Down
5 changes: 2 additions & 3 deletions test/commands/default.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import {describe, it} from 'mocha';

import {CLASP} from '../constants.js';
import {runClasp} from '../functions.js';

describe('Test missing command function', () => {
it('should report missing command correctly', () => {
const result = spawnSync(CLASP, ['parboil'], {encoding: 'utf8'});
const result = runClasp(['parboil']);
const expected = 'Unknown command "clasp parboil"';
expect(result.stderr).to.contain(expected);
expect(result.status).to.equal(1);
Expand Down
10 changes: 4 additions & 6 deletions test/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import {after, before, describe, it} from 'mocha';

import {CLASP} from '../constants.js';
import {cleanup, setup} from '../functions.js';
import {cleanup, runClasp, setup} from '../functions.js';

describe('Test clasp deploy function', () => {
before(setup);
// Could fail to to maximum deployments (20)
// TODO: skip test if at maximum
it('should deploy correctly', () => {
const result = spawnSync(CLASP, ['deploy'], {encoding: 'utf8'});
if (result.stderr) {
const result = runClasp(['deploy']);
if (result.status) {
const err1 = 'Scripts may only have up to 20 versioned deployments at a time';
const err2 = 'Currently just one deployment can be created at a time';
const re = `(?:${err1}|${err2})`;
expect([result.stderr]).to.match(new RegExp(re));
expect(result.stderr).to.match(new RegExp(re));
expect(result.status).to.equal(1);
} else {
expect(result.stdout).to.contain('Created version ');
Expand Down
6 changes: 2 additions & 4 deletions test/commands/deployments.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import {after, before, describe, it} from 'mocha';

import {CLASP} from '../constants.js';
import {cleanup, setup} from '../functions.js';
import {cleanup, runClasp, setup} from '../functions.js';

describe('Test clasp deployments function', () => {
before(setup);
it('should list deployments correctly', () => {
const result = spawnSync(CLASP, ['deployments'], {encoding: 'utf8'});
const result = runClasp(['deployments']);
expect(result.stdout).to.contain('Deployment');
expect(result.status).to.equal(0);
});
Expand Down
8 changes: 3 additions & 5 deletions test/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import {expect} from 'chai';
import {spawnSync} from 'child_process';
import {after, before, describe, it} from 'mocha';

import {CLASP} from '../constants.js';
import {cleanup, setup} from '../functions.js';
import {cleanup, runClasp, setup} from '../functions.js';

describe('Test clasp list function', () => {
before(setup);
it('should list clasp projects correctly', () => {
const result = spawnSync(CLASP, ['list'], {encoding: 'utf8'});
const result = runClasp(['list']);
// Every project starts with this base URL, thus
// using clasp list should at least contain this
// in its output.
expect(result.stdout).to.contain('https://script.google.com/d/');
expect(result.status).to.equal(0);
});
it('does not shorten project names when indicated not to', () => {
const result = spawnSync(CLASP, ['list', '--noShorten'], {encoding: 'utf8'});
const result = runClasp(['list', '--noShorten']);
expect(result.stdout).to.contain('https://script.google.com/d/');
expect(result.stdout).to.not.contain('…');
expect(result.status).to.equal(0);
Expand Down
Loading

0 comments on commit 1c75e06

Please sign in to comment.