Skip to content

Commit

Permalink
tests added in format command
Browse files Browse the repository at this point in the history
  • Loading branch information
catosaurusrex2003 committed Oct 20, 2024
1 parent 314d6b6 commit 843f4be
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ node_modules
test.asyncapi-cli
asyncapi.json
test/fixtures/minimaltemplate/__transpiled
test/fixtures/specification-conv.yml
test/fixtures/specification-conv.yaml
test/fixtures/specification-conv.json
.vscode

/action/
Expand Down
5 changes: 4 additions & 1 deletion src/commands/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ export default class Convert extends Command {
await fPromises.writeFile(finalFileName, formattedFile, {
encoding: 'utf8',
});
this.log(`converted to ${outputFileFormat} at ${green(finalFileName)}`);
this.log(
`succesfully formatted to ${outputFileFormat} at ${green(finalFileName)} ✅`,
);
} else {
this.log(formattedFile);
this.log(`succesfully logged after formatting to ${outputFileFormat} ✅`);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/flags/format.flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const availFileFormats: fileFormat[] = ['yaml', 'yml', 'json'];
export const convertFormatFlags = () => {
return {
help: Flags.help({ char: 'h' }),
output: Flags.string({
char: 'o',
description: 'path to the file where the result is saved',
}),
format: Flags.string({
char: 'f',
description: 'Specify the format to convert to',
options: availFileFormats,
required: true,
default: 'json',
}),
output: Flags.string({
char: 'o',
description: 'path to the file where the result is saved',
}),
};
};
209 changes: 209 additions & 0 deletions test/integration/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import { test } from '@oclif/test';
import { NO_CONTEXTS_SAVED } from '../../src/core/errors/context-error';
import TestHelper, { createMockServer, stopMockServer } from '../helpers';
import { expect } from '@oclif/test';

const testHelper = new TestHelper();
const yamlFilePath = './test/fixtures/specification.yml';
const JSONFilePath = './test/fixtures/specification.json';
const convYmlFilePath = './test/fixtures/specification-conv.yml';
const convYamlFilePath = './test/fixtures/specification-conv.yaml';
const convJSONFilePath = './test/fixtures/specification-conv.json';


describe('format', () => {
describe('with file paths', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

before(() => {
createMockServer();
});

after(() => {
stopMockServer();
});

test
.stderr()
.stdout()
.command(['format', yamlFilePath, '-f', 'json'])
.it('should log formatted content if no -o is passed', (ctx, done) => {
expect(ctx.stdout).to.contain(
'succesfully logged after formatting to json ✅',
);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['format', './test/fixtures/not-found.yml', '-f', 'json'])
.it('should throw error if file path is wrong', (ctx, done) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.equal(
'error loading AsyncAPI document from file: ./test/fixtures/not-found.yml file does not exist.\n',
);
done();
});

test
.stderr()
.stdout()
.command(['format', 'http://localhost:8080/dummySpec.yml', '-f', 'json'])
.it('works when url is passed', (ctx, done) => {
expect(ctx.stdout).to.contain(
'succesfully logged after formatting to json ✅',
);
expect(ctx.stderr).to.equal('');
done();
});
});

describe('with no arguments or required flags', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.setCurrentContext('home');
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
.command(['format', yamlFilePath])
.it('should default to json without -f flag', (ctx, done) => {
expect(ctx.stdout).to.contain(
'succesfully logged after formatting to json ✅',
);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['format', '-f', 'json'])
.it('converts from current context', (ctx, done) => {
expect(ctx.stdout).to.contain(
'succesfully logged after formatting to json ✅',
);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.do(() => {
testHelper.unsetCurrentContext();
testHelper.createDummyContextFile();
})
.command(['format', '-f', 'json'])
.it('throws error message if no current context', (ctx, done) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.equal(
'ContextError: No context is set as current, please set a current context.\n',
);
done();
});
});

describe('with no spec file', () => {
beforeEach(() => {
try {
testHelper.deleteDummyContextFile();
} catch (e: any) {
if (e.code !== 'ENOENT') {
throw e;
}
}
});

test
.stderr()
.stdout()
.command(['format', '-f', 'json'])
.it('throws error message if no spec file exists', (ctx, done) => {
expect(ctx.stdout).to.equal('');
expect(ctx.stderr).to.equal(
`error locating AsyncAPI document: ${NO_CONTEXTS_SAVED}\n`,
);
done();
});
});

describe('format with output flag', () => {
beforeEach(() => {
testHelper.createDummyContextFile();
});

afterEach(() => {
testHelper.deleteDummyContextFile();
});

test
.stderr()
.stdout()
.command(['format', yamlFilePath, '-f', 'json', '-o', convJSONFilePath])
.it('create file yaml -> json', (ctx, done) => {
expect(ctx.stdout).to.contain(
`succesfully formatted to json at ${convJSONFilePath} ✅`,
);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['format', JSONFilePath, '-f', 'yaml', '-o', convYamlFilePath])
.it('create file json -> yaml', (ctx, done) => {
expect(ctx.stdout).to.contain(
`succesfully formatted to yaml at ${convYamlFilePath} ✅`,
);
expect(ctx.stderr).to.equal('');
done();
});

test
.stderr()
.stdout()
.command(['format', JSONFilePath, '-f', 'yml', '-o', convYmlFilePath])
.it('create file json -> yml', (ctx, done) => {
expect(ctx.stdout).to.contain(
`succesfully formatted to yml at ${convYmlFilePath} ✅`,
);
expect(ctx.stderr).to.equal('');
done();
});
});

describe('invalid or redundant format conversions', () => {
test
.stderr()
.stdout()
.command(['format', yamlFilePath, '-f', 'yaml'])
.it('yaml -> yaml', (ctx, done) => {
expect(ctx.stderr).to.contain('Your document is already a YAML');
done();
});

test
.stderr()
.stdout()
.command(['format', JSONFilePath, '-f', 'json'])
.it('json -> json', (ctx, done) => {
expect(ctx.stderr).to.contain('Your document is already a JSON');
done();
});
});
});

0 comments on commit 843f4be

Please sign in to comment.