Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
chore: lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdward162 committed Dec 2, 2020
1 parent 669317f commit 36c57e9
Show file tree
Hide file tree
Showing 14 changed files with 440 additions and 277 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
rules: {
'newline-before-return': 'error',
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
'simple-import-sort/sort': 'error',
'simple-import-sort/imports': 'error',
'sort-imports': 'off',
'import/first': 'error',
'import/newline-after-import': 'error',
Expand Down
3 changes: 1 addition & 2 deletions src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Command, flags } from '@oclif/command';
import { Source } from '@superfaceai/parser';
import * as nodePath from 'path';

import { userError } from '../common/error';

import {
DOCUMENT_PARSE_FUNCTION,
DocumentType,
inferDocumentTypeWithFlag,
} from '../common/document';
import { userError } from '../common/error';
import { DocumentTypeFlag, documentTypeFlag } from '../common/flags';
import { lstatPromise, OutputStream, readFilePromise } from '../common/io';

Expand Down
53 changes: 36 additions & 17 deletions src/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Command, flags } from '@oclif/command';

import {
MAP_EXTENSIONS,
PROFILE_EXTENSIONS,
validateDocumentName,
} from '../common/document';
import { developerError, userError } from '../common/error';

import { MAP_EXTENSIONS, PROFILE_EXTENSIONS, validateDocumentName } from '../common/document';
import { OutputStream } from '../common/io';

import * as profileTemplate from '../templates/profile';
import * as mapTemplate from '../templates/map';
import * as profileTemplate from '../templates/profile';

export default class Create extends Command {
static description = 'Creates empty map and profile on a local filesystem.';
Expand Down Expand Up @@ -36,8 +38,8 @@ export default class Create extends Command {
template: flags.string({
options: ['empty', 'pubs'],
default: 'empty',
description: 'Template to initialize the usecases and maps with'
})
description: 'Template to initialize the usecases and maps with',
}),
};

async run(): Promise<void> {
Expand All @@ -46,7 +48,8 @@ export default class Create extends Command {
let usecases: string[];

if (
typeof documentName !== 'string' || !validateDocumentName(documentName)
typeof documentName !== 'string' ||
!validateDocumentName(documentName)
) {
throw userError('Invalid document name.', 1);
}
Expand All @@ -73,16 +76,32 @@ export default class Create extends Command {
break;
case 'map':
if (!flags.provider) {
throw userError('Provider name must be provided when generating a map.', 2);
throw userError(
'Provider name must be provided when generating a map.',
2
);
}
await this.createMap(documentName, usecases, flags.provider, flags.template);
await this.createMap(
documentName,
usecases,
flags.provider,
flags.template
);
break;
case 'both':
if (!flags.provider) {
throw userError('Provider name must be provided when generating a map.', 2);
throw userError(
'Provider name must be provided when generating a map.',
2
);
}
await this.createProfile(documentName, usecases, flags.template);
await this.createMap(documentName, usecases, flags.provider, flags.template);
await this.createMap(
documentName,
usecases,
flags.provider,
flags.template
);
}
}

Expand All @@ -95,9 +114,10 @@ export default class Create extends Command {
const outputStream = new OutputStream(fileName);

await outputStream.write(
profileTemplate.header(documentName) + useCaseNames.map(
usecase => profileTemplate.usecase(template, usecase)
).join('')
profileTemplate.header(documentName) +
useCaseNames
.map(usecase => profileTemplate.usecase(template, usecase))
.join('')
);
this.log(
`-> Created ${fileName} (id = "https://example.com/profile/${documentName}")`
Expand All @@ -116,9 +136,8 @@ export default class Create extends Command {
const outputStream = new OutputStream(fileName);

await outputStream.write(
mapTemplate.header(documentName, providerName) + useCaseNames.map(
usecase => mapTemplate.map(template, usecase)
).join('')
mapTemplate.header(documentName, providerName) +
useCaseNames.map(usecase => mapTemplate.map(template, usecase)).join('')
);
this.log(
`-> Created ${fileName} (provider = ${providerName}, id = "https://example.com/${providerName}/${documentName}")`
Expand Down
3 changes: 1 addition & 2 deletions src/commands/lint.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Command, flags } from '@oclif/command';
import { Source, SyntaxError } from '@superfaceai/parser';

import { userError, developerError } from '../common/error';

import {
DOCUMENT_PARSE_FUNCTION,
DocumentType,
inferDocumentTypeWithFlag,
} from '../common/document';
import { developerError, userError } from '../common/error';
import { DocumentTypeFlag, documentTypeFlag } from '../common/flags';
import { OutputStream, readFilePromise } from '../common/io';
import { formatWordPlurality } from '../util';
Expand Down
156 changes: 106 additions & 50 deletions src/commands/play.test.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,143 @@
import { stdout } from 'stdout-stderr';
import { accessPromise, mkdirPromise, rimrafPromise, OutputStream } from '../common/io';
import * as nodePath from 'path';
import { stdout } from 'stdout-stderr';

import Play from '../commands/play';
import {
accessPromise,
mkdirPromise,
OutputStream,
rimrafPromise,
} from '../common/io';

describe('Play CLI command', () => {
const baseFixture = nodePath.join('fixtures', 'playgrounds');
const testPlaygroundName = 'test';
const testPlaygroundPath = nodePath.join(baseFixture, testPlaygroundName);
afterEach(async () => {
const baseFixture = nodePath.join('fixtures', 'playgrounds');
const testPlaygroundName = 'test';
const testPlaygroundPath = nodePath.join(baseFixture, testPlaygroundName);

afterEach(async () => {
await rimrafPromise(testPlaygroundPath);

const testFiles = ['package-lock.json', 'node_modules', 'valid.supr.ast.json', 'valid.noop.suma.ast.json', 'valid.noop.js'];

const testFiles = [
'package-lock.json',
'node_modules',
'valid.supr.ast.json',
'valid.noop.suma.ast.json',
'valid.noop.js',
];
await Promise.all(
testFiles.map(
file => rimrafPromise(nodePath.join(baseFixture, 'valid', file))
testFiles.map(file =>
rimrafPromise(nodePath.join(baseFixture, 'valid', file))
)
)
});
);
});

it('a valid playground is detected', async () => {
expect(
await Play.run(['clean', nodePath.join(baseFixture, 'valid')])
).toBeUndefined();
});

it('a valid playground is detected', async () => {
await Play.run(['clean', nodePath.join(baseFixture, 'valid')])
});
it('an invalid playground is rejected', async () => {
await expect(
Play.run(['clean', nodePath.join(baseFixture, 'invalid')])
).rejects.toThrow('The directory at playground path is not a playground');
});

it('an invalid playground is rejected', async () => {
await expect(
Play.run(['clean', nodePath.join(baseFixture, 'invalid')])
).rejects.toThrow('The directory at playground path is not a playground')
});
it('initialize creates a valid playground', async () => {
await Play.run([
'initialize',
testPlaygroundPath,
'--providers',
'foo',
'bar',
]);

it('initialize creates a valid playground', async () => {
await Play.run(['initialize', testPlaygroundPath, '--providers', 'foo', 'bar']);
await accessPromise(testPlaygroundPath);

await accessPromise(testPlaygroundPath);
const expectedFiles = [
'package.json',
'test.supr',
'test.foo.suma',
'test.bar.suma',
'test.foo.ts',
'test.bar.ts',
];
for (const file of expectedFiles) {
await accessPromise(nodePath.join(testPlaygroundPath, file));
}

const expectedFiles = ['package.json', 'test.supr', 'test.foo.suma', 'test.bar.suma', 'test.foo.ts', 'test.bar.ts'];
for (const file of expectedFiles) {
await accessPromise(nodePath.join(testPlaygroundPath, file))
}
// No exceptions thrown
expect(undefined).toBeUndefined();
});

it('execute compiles playground and executes it', async () => {
stdout.start();
await Play.run(['execute', nodePath.join(baseFixture, 'valid'), '--providers', 'noop']);
await Play.run([
'execute',
nodePath.join(baseFixture, 'valid'),
'--providers',
'noop',
]);
stdout.stop();

expect(
stdout.output
).toMatch(/pubs\/Noop result: Ok { value: \[\] }\s*$/);
expect(stdout.output).toMatch(/pubs\/Noop result: Ok { value: \[\] }\s*$/);

const expectedFiles = ['package-lock.json', 'node_modules', 'valid.supr.ast.json', 'valid.noop.suma.ast.json', 'valid.noop.js'];
const expectedFiles = [
'package-lock.json',
'node_modules',
'valid.supr.ast.json',
'valid.noop.suma.ast.json',
'valid.noop.js',
];
await Promise.all(
expectedFiles.map(
file => accessPromise(nodePath.join(baseFixture, 'valid', file))
expectedFiles.map(file =>
accessPromise(nodePath.join(baseFixture, 'valid', file))
)
)
);
}, 30000);

it('clean cleans compilation artifacts', async () => {
const deletedFiles = ['package-lock.json', 'node_modules', 'test.supr.ast.json', 'test.foo.suma.ast.json', 'test.bar.suma.ast.json', 'test.foo.js', 'test.bar.js'];
const expectedFiles = ['package.json', 'test.supr', 'test.foo.suma', 'test.bar.suma', 'test.foo.ts', 'test.bar.ts', 'test.baz.ts'];

it('clean cleans compilation artifacts', async () => {
const deletedFiles = [
'package-lock.json',
'node_modules',
'test.supr.ast.json',
'test.foo.suma.ast.json',
'test.bar.suma.ast.json',
'test.foo.js',
'test.bar.js',
];
const expectedFiles = [
'package.json',
'test.supr',
'test.foo.suma',
'test.bar.suma',
'test.foo.ts',
'test.bar.ts',
'test.baz.ts',
];

await mkdirPromise(testPlaygroundPath);

await Promise.all(
[...deletedFiles, ...expectedFiles].map(
file => OutputStream.writeOnce(nodePath.join(testPlaygroundPath, file), '')
[...deletedFiles, ...expectedFiles].map(file =>
OutputStream.writeOnce(nodePath.join(testPlaygroundPath, file), '')
)
);

await Play.run(['clean', testPlaygroundPath]);

await Promise.all(
deletedFiles.map(
file => expect(accessPromise(nodePath.join(testPlaygroundPath, file))).rejects.toThrowError('ENOENT')
deletedFiles.map(file =>
expect(
accessPromise(nodePath.join(testPlaygroundPath, file))
).rejects.toThrowError('ENOENT')
)
);

await Promise.all(
expectedFiles.map(
file => accessPromise(nodePath.join(testPlaygroundPath, file))
expectedFiles.map(file =>
accessPromise(nodePath.join(testPlaygroundPath, file))
)
)
});
});
);
});
});
Loading

0 comments on commit 36c57e9

Please sign in to comment.