2022-12-14 09:59:42 +00:00
|
|
|
import path from 'canonical-path';
|
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
|
import glob from 'glob';
|
2020-10-15 11:05:14 +00:00
|
|
|
|
2022-12-14 09:59:42 +00:00
|
|
|
import {EXAMPLE_CONFIG_FILENAME, STACKBLITZ_CONFIG_FILENAME} from './constants.mjs';
|
2020-10-15 11:05:14 +00:00
|
|
|
|
2022-12-14 09:59:42 +00:00
|
|
|
import {
|
2020-10-15 11:05:14 +00:00
|
|
|
copyExampleFiles,
|
|
|
|
|
createEmptyExample,
|
|
|
|
|
ensureExamplePath,
|
|
|
|
|
titleize,
|
|
|
|
|
writeExampleConfigFile,
|
2022-12-14 09:59:42 +00:00
|
|
|
writeStackBlitzFile,
|
|
|
|
|
} from './create-example.mjs';
|
2020-10-15 11:05:14 +00:00
|
|
|
|
|
|
|
|
describe('create-example tool', () => {
|
|
|
|
|
describe('createEmptyExample', () => {
|
|
|
|
|
it('should create an empty example with marker files', () => {
|
|
|
|
|
spyOn(fs, 'existsSync').and.returnValue(false);
|
|
|
|
|
spyOn(fs, 'ensureDirSync');
|
|
|
|
|
const writeFileSpy = spyOn(fs, 'writeFileSync');
|
|
|
|
|
|
|
|
|
|
createEmptyExample('foo-bar', '/path/to/foo-bar');
|
|
|
|
|
expect(writeFileSpy).toHaveBeenCalledTimes(2);
|
2022-12-14 09:59:42 +00:00
|
|
|
expect(writeFileSpy).toHaveBeenCalledWith(
|
|
|
|
|
path.resolve(`/path/to/foo-bar/${EXAMPLE_CONFIG_FILENAME}`),
|
|
|
|
|
jasmine.any(String)
|
|
|
|
|
);
|
|
|
|
|
expect(writeFileSpy).toHaveBeenCalledWith(
|
|
|
|
|
path.resolve(`/path/to/foo-bar/${STACKBLITZ_CONFIG_FILENAME}`),
|
|
|
|
|
jasmine.any(String)
|
|
|
|
|
);
|
2020-10-15 11:05:14 +00:00
|
|
|
});
|
2022-11-10 22:25:11 +00:00
|
|
|
|
|
|
|
|
it('should fail if the example name contains spaces', () => {
|
2022-12-14 09:59:42 +00:00
|
|
|
expect(() => createEmptyExample('foo bar', '/path/to/foo-bar')).toThrowError(
|
|
|
|
|
`Unable to create example. The example name contains spaces: 'foo bar'`
|
2022-11-10 22:25:11 +00:00
|
|
|
);
|
|
|
|
|
});
|
2020-10-15 11:05:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('ensureExamplePath', () => {
|
|
|
|
|
it('should error if the path already exists', () => {
|
|
|
|
|
spyOn(fs, 'existsSync').and.returnValue(true);
|
2022-12-14 09:59:42 +00:00
|
|
|
expect(() => ensureExamplePath('foo/bar')).toThrowError(
|
|
|
|
|
`Unable to create example. The path to the new example already exists: foo/bar`
|
|
|
|
|
);
|
2020-10-15 11:05:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should create the directory on disk', () => {
|
|
|
|
|
spyOn(fs, 'existsSync').and.returnValue(false);
|
|
|
|
|
const spy = spyOn(fs, 'ensureDirSync');
|
|
|
|
|
ensureExamplePath('foo/bar');
|
|
|
|
|
expect(spy).toHaveBeenCalledWith('foo/bar');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('writeExampleConfigFile', () => {
|
|
|
|
|
it('should write a JSON file to disk', () => {
|
|
|
|
|
const spy = spyOn(fs, 'writeFileSync');
|
|
|
|
|
writeExampleConfigFile('/foo/bar');
|
2021-04-27 19:50:11 +00:00
|
|
|
expect(spy).toHaveBeenCalledWith(path.resolve(`/foo/bar/${EXAMPLE_CONFIG_FILENAME}`), '');
|
2020-10-15 11:05:14 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('writeStackBlitzFile', () => {
|
|
|
|
|
it('should write a JSON file to disk', () => {
|
|
|
|
|
const spy = spyOn(fs, 'writeFileSync');
|
|
|
|
|
writeStackBlitzFile('bar-bar', '/foo/bar-bar');
|
2022-12-14 09:59:42 +00:00
|
|
|
expect(spy).toHaveBeenCalledWith(
|
|
|
|
|
path.resolve(`/foo/bar-bar/${STACKBLITZ_CONFIG_FILENAME}`),
|
|
|
|
|
[
|
|
|
|
|
'{',
|
|
|
|
|
' "description": "Bar Bar",',
|
|
|
|
|
' "files": [',
|
|
|
|
|
' "!**/*.d.ts",',
|
|
|
|
|
' "!**/*.js",',
|
|
|
|
|
' "!**/*.[1,2].*"',
|
|
|
|
|
' ],',
|
|
|
|
|
' "tags": [',
|
|
|
|
|
' [',
|
|
|
|
|
' "bar",',
|
|
|
|
|
' "bar"',
|
|
|
|
|
' ]',
|
|
|
|
|
' ]',
|
|
|
|
|
'}',
|
|
|
|
|
'',
|
|
|
|
|
].join('\n')
|
|
|
|
|
);
|
2020-10-15 11:05:14 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('copyExampleFiles', () => {
|
|
|
|
|
it('should copy over files that are not ignored by git', () => {
|
|
|
|
|
const sourceGitIgnorePath = path.resolve('/source/path', '.gitignore');
|
|
|
|
|
|
|
|
|
|
spyOn(console, 'log');
|
|
|
|
|
spyOn(fs, 'existsSync').and.returnValue(true);
|
2022-12-14 09:59:42 +00:00
|
|
|
const readFileSyncSpy = spyOn(fs, 'readFileSync').and.callFake((p) => {
|
2020-10-15 11:05:14 +00:00
|
|
|
switch (p) {
|
|
|
|
|
case sourceGitIgnorePath:
|
|
|
|
|
return '**/*.bad';
|
|
|
|
|
default:
|
|
|
|
|
throw new Error('Unexpected path');
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-12-14 09:59:42 +00:00
|
|
|
spyOn(glob, 'sync').and.returnValue(['a/', 'a/b/', 'a/c', 'x.ts', 'x.bad']);
|
2020-10-15 11:05:14 +00:00
|
|
|
const ensureDirSyncSpy = spyOn(fs, 'ensureDirSync');
|
|
|
|
|
const copySyncSpy = spyOn(fs, 'copySync');
|
|
|
|
|
|
|
|
|
|
copyExampleFiles('/source/path', '/path/to/test-example', 'test-example');
|
|
|
|
|
|
|
|
|
|
expect(readFileSyncSpy).toHaveBeenCalledWith(sourceGitIgnorePath, 'utf8');
|
|
|
|
|
|
|
|
|
|
expect(ensureDirSyncSpy.calls.allArgs()).toEqual([
|
2021-04-27 19:50:11 +00:00
|
|
|
[path.resolve('/path/to/test-example/a')],
|
|
|
|
|
[path.resolve('/path/to/test-example')],
|
2020-10-15 11:05:14 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect(copySyncSpy.calls.allArgs()).toEqual([
|
2021-04-27 19:50:11 +00:00
|
|
|
[path.resolve('/source/path/a/c'), path.resolve('/path/to/test-example/a/c')],
|
|
|
|
|
[path.resolve('/source/path/x.ts'), path.resolve('/path/to/test-example/x.ts')],
|
2020-10-15 11:05:14 +00:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('titleize', () => {
|
|
|
|
|
it('should convert a kebab-case string to title-case', () => {
|
|
|
|
|
expect(titleize('abc')).toEqual('Abc');
|
|
|
|
|
expect(titleize('abc-def')).toEqual('Abc Def');
|
|
|
|
|
expect(titleize('123')).toEqual('123');
|
|
|
|
|
expect(titleize('abc---def')).toEqual('Abc - Def');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|