Skip to content

Commit

Permalink
chore(cli): add git test for lb4 copyright
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Apr 1, 2020
1 parent c8872ec commit 56029c5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cli/generators/copyright/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const cache = new Map();
async function git(cwd, ...args) {
const cmd = 'git ' + util.format(...args);
const key = `${cwd}:${cmd}`;
debug('Running %s', cmd);
debug('Running %s in directory', cmd, cwd);
if (cache.has(key)) {
return cache.get(key);
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/generators/copyright/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ async function jsOrTsFiles(cwd, paths = []) {
* @param {object} options - Options
*/
async function updateFileHeaders(projectRoot, options = {}) {
debug('Starting project root: %s', projectRoot);
options = {
dryRun: false,
gitOnly: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/generators/copyright/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = class CopyrightGenerator extends BaseGenerator {
id.toLocaleLowerCase(),
)
) {
// Add well-known licenses in front of the list
this.licenseList.unshift(license);
} else {
this.licenseList.push(license);
Expand All @@ -34,6 +35,7 @@ module.exports = class CopyrightGenerator extends BaseGenerator {
}

initializing() {
// Register `autocomplete` plugin
this.env.adapter.promptModule.registerPrompt('autocomplete', autocomplete);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const path = require('path');
const fs = require('fs-extra');
const assert = require('yeoman-assert');
const git = require('../../../generators/copyright/git');

const generator = path.join(__dirname, '../../../generators/copyright');
const {spdxLicenseList} = require('../../../generators/copyright/header');
const FIXTURES = path.join(__dirname, '../../fixtures/copyright');
const LOCATION = 'single-package';
const PROJECT_ROOT = path.join(FIXTURES, LOCATION);
const testUtils = require('../../test-utils');

// Establish the year(s)
let year = new Date().getFullYear();
if (year !== 2020) year = `2020,${year}`;

describe('lb4 copyright with git', function() {
// eslint-disable-next-line no-invalid-this
this.timeout(30000);

before('add files not tracked by git', async () => {
await fs.outputFile(
path.join(PROJECT_ROOT, '.sandbox/file-not-tracked.js'),
`
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
function test() {}
`,
);
});

after('reset sandbox', async () => {
await fs.remove(path.join(PROJECT_ROOT, '.sandbox'));
});

after('reset local changes ', async () => {
// Revert changes made by the test against git-tracked fixtures
await git(FIXTURES, `checkout -- ${LOCATION}`);
});

it('updates copyright/license headers with options', async () => {
await testUtils
.executeGenerator(generator)
.cd(PROJECT_ROOT)
.withOptions({
owner: 'ACME Inc.',
license: 'ISC',
gitOnly: true,
// Set `localConfigOnly` to skip searching for `.yo-rc.json` to change
// the destination root
localConfigOnly: true,
});

// git tracked files are changed
assertHeader(
['src/application.ts', 'lib/no-header.js'],
`// Copyright ACME Inc. ${year}. All Rights Reserved.`,
'// Node module: myapp',
`// This file is licensed under the ${spdxLicenseList['isc'].name}.`,
`// License text available at ${spdxLicenseList['isc'].url}`,
);

// non git-tracked files are not touched
assertHeader(
['.sandbox/file-not-tracked.js'],
'// Copyright IBM Corp. 2020. All Rights Reserved.',
'// Node module: @loopback/cli',
'// This file is licensed under the MIT License.',
'// License text available at https://opensource.org/licenses/MIT',
);
});
});

function assertHeader(fileNames, ...expected) {
if (typeof fileNames === 'string') {
fileNames = [fileNames];
}
for (const f of fileNames) {
const file = path.join(FIXTURES, LOCATION, f);
for (const line of expected) {
assert.fileContent(file, line);
}
}
}

0 comments on commit 56029c5

Please sign in to comment.