Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/domains/models/SpecificationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ export async function fileExists(name: string): Promise<boolean> {
return true;
}

const extension = name.split('.')[1];
// Get the last segment after the last dot to handle multi-dot filenames
// e.g., 'my.asyncapi.yaml' should yield 'yaml', not 'asyncapi'
const splitParts = name.split('.');
const extension = splitParts.length > 1 ? splitParts.pop() : '';

const allowedExtenstion = ['yml', 'yaml', 'json'];

Expand Down
86 changes: 86 additions & 0 deletions test/unit/models/spec-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { expect } from 'chai';
import { promises as fs } from 'fs';
import path from 'path';
import os from 'os';
import { fileExists } from '../../../src/domains/models/SpecificationFile';

describe('fileExists()', () => {
const tmpDir = path.join(os.tmpdir(), 'asyncapi-cli-test-' + Date.now());
const files: string[] = [];

before(async () => {
await fs.mkdir(tmpDir, { recursive: true });
});

after(async () => {
for (const f of files) {
try { await fs.unlink(f); } catch { /* best effort */ }
}
try { await fs.rmdir(tmpDir); } catch { /* best effort */ }
});

function tmpFile(name: string): string {
const p = path.join(tmpDir, name);
files.push(p);
return p;
}

it('returns true for a valid .yaml file', async () => {
const p = tmpFile('simple.yaml');
await fs.writeFile(p, 'asyncapi: 3.0.0', 'utf8');
expect(await fileExists(p)).to.equal(true);
});

it('returns true for a valid .yml file', async () => {
const p = tmpFile('simple.yml');
await fs.writeFile(p, 'asyncapi: 3.0.0', 'utf8');
expect(await fileExists(p)).to.equal(true);
});

it('returns true for a valid .json file', async () => {
const p = tmpFile('simple.json');
await fs.writeFile(p, '{"asyncapi":"3.0.0"}', 'utf8');
expect(await fileExists(p)).to.equal(true);
});

it('handles multi-dot filenames like my.asyncapi.yaml', async () => {
const p = tmpFile('my.asyncapi.yaml');
await fs.writeFile(p, 'asyncapi: 3.0.0', 'utf8');
expect(await fileExists(p)).to.equal(true);
});

it('handles multi-dot filenames like my.api.spec.yml', async () => {
const p = tmpFile('my.api.spec.yml');
await fs.writeFile(p, 'asyncapi: 3.0.0', 'utf8');
expect(await fileExists(p)).to.equal(true);
});

it('handles multi-dot filenames like api.v2.spec.json', async () => {
const p = tmpFile('api.v2.spec.json');
await fs.writeFile(p, '{"asyncapi":"3.0.0"}', 'utf8');
expect(await fileExists(p)).to.equal(true);
});

it('rejects files with unsupported extensions', async () => {
const p = tmpFile('readme.txt');
await fs.writeFile(p, 'hello', 'utf8');
// fileExists should throw an ErrorLoadingSpec for invalid files
try {
await fileExists(p);
expect.fail('expected throw for .txt file');
} catch (e: unknown) {
expect((e as Error).message).to.match(/invalid|Unable|not a valid/i);
}
});

it('rejects files with only one segment (no extension)', async () => {
const p = tmpFile('noextension');
await fs.writeFile(p, 'asyncapi: 3.0.0', 'utf8');
try {
await fileExists(p);
expect.fail('expected throw for file with no extension');
} catch (e: unknown) {
expect((e as Error).message).to.match(/invalid|Unable|not a valid/i);
}
});
});