Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clasp status tests and --json option #167

Merged
merged 4 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ node_modules/
.npm
.env
.nyc_output
*.lock
*.lock
/tmp
22 changes: 12 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,23 +442,25 @@ commander
commander
.command('status')
.description('Lists files that will be pushed by clasp')
.action(async () => {
.option('--json', "Show status in JSON form")
.action(async (cmd: { json: boolean }) => {
await checkIfOnline();
getProjectSettings().then(({ scriptId, rootDir }: ProjectSettings) => {
if (!scriptId) return;
getProjectFiles(rootDir, (err, projectFiles) => {
if(err) return console.log(err);
else if (projectFiles) {
const [nonIgnoredFilePaths, ignoredFilePaths] = projectFiles;
console.log(LOG.STATUS_PUSH);
nonIgnoredFilePaths.map((filePath: string) => {
console.log(`└─ ${filePath}`);
});
if (ignoredFilePaths.length) {
const status = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this format.
const [filesToPush, untrackedFiles] = projectFiles;

Below use:

if (cmd.json) {
  console.log(JSON.stringify({ filesToPush, untrackedFiles});

filesToPush: projectFiles[0],
untrackedFiles: projectFiles[1],
};
if (cmd.json) {
console.log(JSON.stringify(status));
} else {
console.log(LOG.STATUS_PUSH);
status.filesToPush.forEach((file) => console.log(`└─ ${file}`));
console.log(LOG.STATUS_IGNORE);
ignoredFilePaths.map((filePath: string) => {
console.log(`└─ ${filePath}`);
});
status.untrackedFiles.forEach((file) => console.log(`└─ ${file}`));
}
}
});
Expand Down
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,27 @@
},
"devDependencies": {
"@types/anymatch": "^1.3.0",
"@types/chai": "^4.1.3",
"@types/cli-spinner": "^0.2.0",
"@types/commander": "^2.12.2",
"@types/connect": "^3.4.31",
"@types/del": "^3.0.0",
"@types/events": "^1.1.0",
"@types/fs-extra": "^5.0.2",
"@types/glob": "^5.0.35",
"@types/mkdirp": "^0.5.2",
"@types/mocha": "^5.2.0",
"@types/node": "^9.4.0",
"@types/open": "^0.0.29",
"@types/pluralize": "^0.0.28",
"@types/recursive-readdir": "^2.2.0",
"@types/tmp": "0.0.33",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"fs-extra": "^6.0.1",
"mocha": "^5.1.0",
"nyc": "^11.6.0",
"tmp": "0.0.33",
"tslint": "^5.9.1",
"typescript": "^2.8.1"
}
Expand Down
48 changes: 45 additions & 3 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import * as fs from 'fs';
import * as fs from 'fs-extra';
import * as os from 'os';

const { spawnSync } = require('child_process');
import { getScriptURL, getFileType, getAPIFileType,
saveProjectId } from './../src/utils.js';
const path = require('path');
import * as path from 'path';
import * as tmp from 'tmp';

describe('Test help for each function', () => {
it('should output help for run command', () => {
Expand All @@ -18,7 +19,7 @@ describe('Test help for each function', () => {
});
it('should output help for logs command', () => {
const result = spawnSync(
'clasp', ['logs', '--help'], { encoding : 'utf8', detached: true },
'clasp', ['logs', '--help'], { encoding : 'utf8' },
);
expect(result.status).to.equal(0);
expect(result.stdout).to.include('Shows the StackDriver logs');
Expand Down Expand Up @@ -106,6 +107,47 @@ describe.skip('Test clasp push function', () => {
});
});

describe.skip('Test clasp status function', () => {
function setupTmpDirectory(filepathsAndContents: Array<{ file: string, data: string }>) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why we need a /tmp directory for testing. We've been using fs.writeFileSync('.clasprc.json', ' '); if we need files for testing. Should we be changing that in the other tests as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the other tests using fs.writeFileSync directly actually just end up dumping files into the /clasp directory. It'd probably be better to have all the test related file writing in tmp/ so we can clean up nicely afterwards.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

fs.ensureDirSync('tmp');
const tmpdir = tmp.dirSync({ unsafeCleanup: true, dir: 'tmp/', keep: true }).name;
filepathsAndContents.forEach(({ file, data }) => {
fs.outputFileSync(path.join(tmpdir, file), data);
});
return tmpdir;
}
it("should respect globs and negation rules", () => {
const tmpdir = setupTmpDirectory([
{ file: '.claspignore', data: '**/**\n!build/main.js\n!appsscript.json' },
{ file: 'build/main.js', data: ' ' },
{ file: 'appsscript.json', data: ' ' },
{ file: 'shouldBeIgnored', data: ' ' },
{ file: 'should/alsoBeIgnored', data: ' ' },
]);
spawnSync('clasp', ['create', '[TEST] clasp status'], { encoding: 'utf8', cwd: tmpdir });
const result = spawnSync('clasp', ['status', '--json'], { encoding: 'utf8', cwd: tmpdir });
expect(result.status).to.equal(0);
const resultJson = JSON.parse(result.stdout);
expect(resultJson.untrackedFiles).to.have.members(['shouldBeIgnored', 'should/alsoBeIgnored']);
expect(resultJson.filesToPush).to.have.members(['build/main.js', 'appsscript.json']);
});
// https://github.com/google/clasp/issues/67 - This test currently fails
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After each test, maybe add fs.removeSync('/tmp'); to clean up a bit?

Copy link
Contributor Author

@ajlai ajlai May 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keep: false here cleans up tmpdir after the tests finish: https://github.com/google/clasp/pull/167/files#diff-9c706fb89ddc3725ae3168d13b1298eaR113

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks for changing that.

it.skip('should ignore dotfiles if the parent folder is ignored', () => {
const tmpdir = setupTmpDirectory([
{ file: '.claspignore', data: '**/node_modules/**\n**/**\n!appsscript.json' },
{ file: 'appsscript.json', data: ' ' },
{ file: 'node_modules/fsevents/build/Release/.deps/Release/.node.d', data: ' ' },
]);
spawnSync('clasp', ['create', '[TEST] clasp status'], { encoding: 'utf8', cwd: tmpdir });
const result = spawnSync('clasp', ['status', '--json'], { encoding: 'utf8', cwd: tmpdir });
expect(result.status).to.equal(0);
const resultJson = JSON.parse(result.stdout);
expect(resultJson.untrackedFiles).to.have.members([
'node_modules/fsevents/build/Release/.deps/Release/.node.d']);
expect(resultJson.filesToPush).to.have.members(['appsscript.json']);
});
});

describe.skip('Test clasp open function', () => {
it('should open a project correctly', () => {
const result = spawnSync(
Expand Down