-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from cmars/feat/auto-discover-vervet-resource…
…s-dir feat: automatically discover default vervet resources directory
- Loading branch information
Showing
7 changed files
with
714 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,106 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
import * as os from "os"; | ||
import { loadVervetResoucePaths } from "../vervet-resolver"; | ||
|
||
describe("vervet resolver", () => { | ||
const originalwd = process.cwd(); | ||
let tmpDir = ""; | ||
|
||
beforeEach(() => { | ||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "foo-")); | ||
fs.writeFileSync( | ||
path.join(tmpDir, ".vervet.yaml"), | ||
` | ||
apis: | ||
testapi: | ||
resources: | ||
- path: 'src/testapi/something/something/resources' | ||
- path: 'src/testapi/something-else/something-else/resources' | ||
testapi2: | ||
resources: | ||
- path: 'src/testapi2/resources' | ||
`, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
process.chdir(originalwd); | ||
if (tmpDir !== "") { | ||
fs.rmSync(tmpDir, { force: true, recursive: true }); | ||
} | ||
}); | ||
|
||
it("chooses the first resource path in the first API", async () => { | ||
// arrange | ||
process.chdir(tmpDir); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths(); | ||
// assert | ||
expect(vervetPaths?.defaultResourcesPath).toMatch( | ||
new RegExp(".*src/testapi/something/something/resources$"), | ||
); | ||
}); | ||
|
||
it("works from a subdirectory in the same project", async () => { | ||
// arrange | ||
const otherDir = path.join(tmpDir, "other-place"); | ||
fs.mkdirSync(otherDir); | ||
process.chdir(otherDir); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths(); | ||
// assert | ||
expect(vervetPaths?.defaultResourcesPath).toMatch( | ||
new RegExp(".*src/testapi/something/something/resources$"), | ||
); | ||
}); | ||
|
||
it("requires a vervet config", async () => { | ||
// arrange | ||
process.chdir(tmpDir); | ||
fs.unlinkSync(".vervet.yaml"); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths(); | ||
// assert | ||
expect(vervetPaths).toBeFalsy(); | ||
}); | ||
|
||
it("requires an api to be defined", async () => { | ||
// arrange | ||
process.chdir(tmpDir); | ||
fs.writeFileSync(".vervet.yaml", "apis: {}"); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths(); | ||
// assert | ||
expect(vervetPaths).toBeFalsy(); | ||
}); | ||
|
||
it("requires a valid vervet config", async () => { | ||
// arrange | ||
process.chdir(tmpDir); | ||
fs.writeFileSync(".vervet.yaml", "}}}bad wolf{{{"); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths(); | ||
// assert | ||
expect(vervetPaths).toBeFalsy(); | ||
}); | ||
|
||
it("requires target directory to exist", async () => { | ||
// arrange | ||
process.chdir(tmpDir); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths("/does-not-exist"); | ||
// assert | ||
expect(vervetPaths).toBeFalsy(); | ||
}); | ||
|
||
it("requires apis to declare resources", async () => { | ||
// arrange | ||
process.chdir(tmpDir); | ||
fs.writeFileSync(".vervet.yaml", "apis: {foo: {}}"); | ||
// act | ||
const vervetPaths = await loadVervetResoucePaths("/does-not-exist"); | ||
// assert | ||
expect(vervetPaths).toBeFalsy(); | ||
}); | ||
}); |
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,71 @@ | ||
import findParentDir from "find-parent-dir"; | ||
import * as path from "path"; | ||
import * as fs from "fs/promises"; | ||
import * as util from "util"; | ||
import { loadYaml } from "@useoptic/openapi-io"; | ||
|
||
const findParentDirAsync = util.promisify(findParentDir); | ||
|
||
/** | ||
* VervetResourcePaths models the resource paths defined in a Vervet project | ||
* configuration file. | ||
* | ||
* For more information, see https://pkg.go.dev/github.com/snyk/vervet/v4/config#Project | ||
*/ | ||
export class VervetResourcePaths { | ||
public readonly defaultResourcesPath: string; | ||
public readonly resourcesPaths: Set<string>; | ||
public readonly projectRoot: string; | ||
|
||
/** | ||
* Create a new instance given the projectRoot directory and Vervet | ||
* configuration document object. | ||
*/ | ||
constructor(projectRoot: string, doc: any) { | ||
this.projectRoot = projectRoot; | ||
const apis = doc?.apis; | ||
if (!apis) { | ||
throw new InvalidVervetConfig(); | ||
} | ||
const resourcesPaths: string[] = []; | ||
for (const apiName in apis) { | ||
const resources: Array<{ path: string }> = apis[apiName]?.resources ?? []; | ||
resources | ||
.filter((rc) => rc && rc.path && rc.path !== "") | ||
.forEach((rc) => { | ||
resourcesPaths.push(rc.path); | ||
}); | ||
} | ||
if (resourcesPaths.length == 0) { | ||
throw new InvalidVervetConfig(); | ||
} | ||
this.defaultResourcesPath = resourcesPaths[0]; | ||
this.resourcesPaths = new Set(resourcesPaths); | ||
} | ||
} | ||
|
||
class InvalidVervetConfig extends Error {} | ||
|
||
export const loadVervetResoucePaths = async ( | ||
fromDir = ".", | ||
): Promise<VervetResourcePaths | null> => { | ||
const vervetConfDir = await findParentDirAsync( | ||
path.resolve(fromDir), | ||
".vervet.yaml", | ||
); | ||
if (!vervetConfDir) { | ||
return null; | ||
} | ||
const vervetConfPath = path.join(vervetConfDir, ".vervet.yaml"); | ||
if (!vervetConfPath) { | ||
return null; | ||
} | ||
try { | ||
const vervetConfYaml = await fs.readFile(vervetConfPath); | ||
const vervetDoc = loadYaml(vervetConfYaml.toString()); | ||
const vervetConfig = new VervetResourcePaths(vervetConfDir, vervetDoc); | ||
return vervetConfig; | ||
} catch (_err) { | ||
return null; | ||
} | ||
}; |
Oops, something went wrong.