-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(services): add function to search oas non recursively
- This function will search oas when the origin is local - The recursive version will search when the origin is remote - Search for oas recursively skips files/dirs like '.git/, .husky/' Refs #20
- Loading branch information
1 parent
bd38b7c
commit 32244e0
Showing
4 changed files
with
157 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import path from 'node:path'; | ||
import fs from 'node:fs'; | ||
import { stub, restore } from 'sinon'; | ||
import { findOasFromDir, findOasFromDirRecursive, oasUtils } from '../../../src/services/find-oas-from-dir.js'; | ||
import { expect } from 'chai'; | ||
|
||
describe('unit:find-oas-from-dir', () => { | ||
describe('findOasFromDir', () => { | ||
let existsSyncStub; | ||
let readdirSyncStub; | ||
let isOasStub; | ||
let joinStub; | ||
beforeEach(() => { | ||
existsSyncStub = stub(fs, 'existsSync'); | ||
readdirSyncStub = stub(fs, 'readdirSync'); | ||
isOasStub = stub(oasUtils, 'isOas'); | ||
joinStub = stub(path, 'join'); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('should return empty array if no dir', async () => { | ||
const logStub = stub(console, 'log'); | ||
logStub.returns(); | ||
existsSyncStub.returns(false); | ||
const result = await findOasFromDir('foo'); | ||
expect(result).to.be.an('array').that.is.empty; | ||
}); | ||
|
||
it('should return an array of oas files', async () => { | ||
existsSyncStub.returns(true); | ||
readdirSyncStub.returns(['foo.yaml', 'bar.yaml']); | ||
isOasStub.returns(true); | ||
joinStub.returns('foo/bar.yaml'); | ||
const result = await findOasFromDir('path/to/dir'); | ||
expect(result).to.be.an('array').that.has.lengthOf(2); | ||
}); | ||
it('should return an empty array if no oas files', async () => { | ||
existsSyncStub.returns(true); | ||
readdirSyncStub.returns(['foo.yaml', 'bar.yaml']); | ||
isOasStub.returns(false); | ||
joinStub.returns('foo/bar.yaml'); | ||
const result = await findOasFromDir('path/to/dir'); | ||
expect(result).to.be.an('array').that.is.empty; | ||
}); | ||
|
||
it('should return only yaml files', async () => { | ||
existsSyncStub.returns(true); | ||
readdirSyncStub.returns(['foo.txt', 'bar.yaml']); | ||
isOasStub.returns(true); | ||
joinStub.returns('foo/bar.yaml'); | ||
const result = await findOasFromDir('path/to/dir'); | ||
expect(result).to.be.an('array').that.has.lengthOf(1); | ||
}); | ||
}); | ||
|
||
describe('findOasFromDirRecursive', () => { | ||
let existsSyncStub; | ||
let readdirSyncStub; | ||
let isOasStub; | ||
let joinStub; | ||
let lstatSyncStub; | ||
beforeEach(() => { | ||
existsSyncStub = stub(fs, 'existsSync'); | ||
readdirSyncStub = stub(fs, 'readdirSync'); | ||
isOasStub = stub(oasUtils, 'isOas'); | ||
joinStub = stub(path, 'join'); | ||
lstatSyncStub = stub(fs, 'lstatSync'); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
it('should return empty array if no dir', async () => { | ||
const logStub = stub(console, 'log'); | ||
logStub.returns(); | ||
existsSyncStub.returns(false); | ||
const result = await findOasFromDirRecursive('foo'); | ||
expect(result).to.be.an('array').that.is.empty; | ||
}); | ||
|
||
it('should search on subdirectories', async () => { | ||
existsSyncStub.returns(true); | ||
readdirSyncStub.onCall(0).returns(['subdir']); | ||
readdirSyncStub.onCall(1).returns(['foo.yaml', 'bar.yaml']); | ||
lstatSyncStub.onCall(0).returns({ isDirectory: () => true }); | ||
lstatSyncStub.onCall(1).returns({ isDirectory: () => false }); | ||
lstatSyncStub.onCall(2).returns({ isDirectory: () => false }); | ||
isOasStub.returns(true); | ||
joinStub.returns('foo/bar.yaml'); | ||
const result = await findOasFromDirRecursive('path/to/dir'); | ||
expect(result).to.be.an('array').that.has.lengthOf(2); | ||
}); | ||
|
||
it('should return only oas files', async () => { | ||
existsSyncStub.returns(true); | ||
readdirSyncStub.onCall(0).returns(['subdir']); | ||
readdirSyncStub.onCall(1).returns(['foo.yaml', 'bar.yaml']); | ||
lstatSyncStub.onCall(0).returns({ isDirectory: () => true }); | ||
lstatSyncStub.onCall(1).returns({ isDirectory: () => false }); | ||
lstatSyncStub.onCall(2).returns({ isDirectory: () => false }); | ||
isOasStub.onCall(0).returns(true); | ||
isOasStub.onCall(1).returns(false); | ||
joinStub.returns('foo/bar.yaml'); | ||
const result = await findOasFromDirRecursive('path/to/dir'); | ||
expect(result).to.be.an('array').that.has.lengthOf(1); | ||
}); | ||
|
||
it('should return only yaml files', async () => { | ||
existsSyncStub.returns(true); | ||
readdirSyncStub.onCall(0).returns(['subdir']); | ||
readdirSyncStub.onCall(1).returns(['foo.txt', 'bar.yaml']); | ||
lstatSyncStub.onCall(0).returns({ isDirectory: () => true }); | ||
lstatSyncStub.onCall(1).returns({ isDirectory: () => false }); | ||
lstatSyncStub.onCall(2).returns({ isDirectory: () => false }); | ||
isOasStub.returns(true); | ||
joinStub.returns('foo/bar.yaml'); | ||
const result = await findOasFromDirRecursive('path/to/dir'); | ||
expect(result).to.be.an('array').that.has.lengthOf(1); | ||
}); | ||
}); | ||
}); |