Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions test/integration/optimize.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import os from 'os';
import { test } from '@oclif/test';
import TestHelper, { createMockServer, stopMockServer } from '../helpers/index';
import fs from 'fs-extra';
Expand All @@ -12,6 +13,9 @@ const unoptimizedYamlFile = './test/fixtures/dummyspec/unoptimizedSpec.yml';
const unoptimizedJsonFile = './test/fixtures/dummyspec/unoptimizedSpec.json';
const invalidFile = './test/fixtures/specification-invalid.yml';
const asyncapiv3 = './test/fixtures/specification-v3.yml';
// Stable temp paths (evaluated at load time so oclif `.command([...])` captures them).
const tempOverwriteYamlFile = path.join(os.tmpdir(), `asyncapi-optimize-overwrite-${process.pid}.yml`);
const tempOverwriteJsonFile = path.join(os.tmpdir(), `asyncapi-optimize-overwrite-${process.pid}.json`);

describe('optimize', () => {
describe('no optimization needed', () => {
Expand Down Expand Up @@ -129,6 +133,11 @@ describe('optimize', () => {
});

describe('no-tty flag', () => {
afterEach(() => {
fs.removeSync(tempOverwriteYamlFile);
fs.removeSync(tempOverwriteJsonFile);
});

test
.stderr()
.stdout()
Expand Down Expand Up @@ -166,6 +175,42 @@ describe('optimize', () => {
fs.unlinkSync(optimizedFile);
done();
});

test
.stderr()
.stdout()
.do(() => {
fs.copySync(unoptimizedYamlFile, tempOverwriteYamlFile);
})
.command(['optimize', tempOverwriteYamlFile, '--no-tty', '-o', 'overwrite'])
.it('overwrite original YAML file in place with --output=overwrite', (ctx, done) => {
const originalContent = fs.readFileSync(unoptimizedYamlFile, 'utf8');
const updatedContent = fs.readFileSync(tempOverwriteYamlFile, 'utf8');

expect(ctx.stdout).to.contain(`✅ Success! Your original file at ${tempOverwriteYamlFile} has been updated.`);
expect(ctx.stderr).to.equal('');
expect(updatedContent).to.contain('asyncapi: 2.0.0');
expect(updatedContent).to.not.equal(originalContent);
done();
});

test
.stderr()
.stdout()
.do(() => {
fs.copySync(unoptimizedJsonFile, tempOverwriteJsonFile);
})
.command(['optimize', tempOverwriteJsonFile, '--no-tty', '-o', 'overwrite'])
.it('overwrite original JSON file in place with --output=overwrite', (ctx, done) => {
const originalContent = fs.readFileSync(unoptimizedJsonFile, 'utf8');
const updatedContent = fs.readFileSync(tempOverwriteJsonFile, 'utf8');

expect(ctx.stdout).to.contain(`✅ Success! Your original file at ${tempOverwriteJsonFile} has been updated.`);
expect(ctx.stderr).to.equal('');
expect(updatedContent).to.contain('"asyncapi": "2.0.0"');
expect(updatedContent).to.not.equal(originalContent);
done();
});
});

describe('interactive terminal', () => {
Expand Down
Loading