refactor test
This commit is contained in:
Родитель
b41ac48698
Коммит
a2f1820f3e
|
@ -92,3 +92,6 @@ typings/
|
|||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# vscode
|
||||
.vscode
|
|
@ -50,12 +50,14 @@
|
|||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/mock-fs": "^3.6.30",
|
||||
"azure-devops-node-api": "^9.0.1",
|
||||
"cli-table": "^0.3.1",
|
||||
"commander": "^3.0.1",
|
||||
"cp-file": "^7.0.0",
|
||||
"js-yaml": "^3.13.1",
|
||||
"node-emoji": "^1.10.0",
|
||||
"mock-fs": "^4.10.1",
|
||||
"shelljs": "^0.8.3",
|
||||
"spektate": "^0.1.5",
|
||||
"uuid": "^3.3.3",
|
||||
|
|
|
@ -61,15 +61,9 @@ export const createCommandDecorator = (command: commander.Command): void => {
|
|||
`maintainerEmail must be of type 'string', ${typeof maintainerEmail} given.`
|
||||
);
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
await createService(projectPath, serviceName, {
|
||||
maintainerEmail,
|
||||
maintainerName
|
||||
=======
|
||||
await createService(projectPath, serviceName, packagesDir, {
|
||||
maintainerName,
|
||||
maintainerEmail
|
||||
>>>>>>> maintainers.yaml being updated when creating/adding service
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error(
|
||||
|
|
|
@ -1,14 +1,11 @@
|
|||
import fs from "fs";
|
||||
import os from "os";
|
||||
import path from "path";
|
||||
import uuid from "uuid/v4";
|
||||
import mockFs from "mock-fs";
|
||||
|
||||
import yaml from "js-yaml";
|
||||
import { promisify } from "util";
|
||||
import { MockFactory } from "../test/mockFactory";
|
||||
|
||||
import cpFile from "cp-file";
|
||||
|
||||
import { IMaintainersFile, IUser } from "../types";
|
||||
import { disableVerboseLogging, enableVerboseLogging, logger } from "../logger";
|
||||
import { IMaintainersFile, IUser } from "../types";
|
||||
import { addNewServiceToMaintainersFile } from "./fileutils";
|
||||
|
||||
beforeAll(() => {
|
||||
|
@ -19,79 +16,52 @@ afterAll(() => {
|
|||
disableVerboseLogging();
|
||||
});
|
||||
|
||||
describe("Adding a new maintainer to existing maintainers file", () => {
|
||||
test("Existing maintainer, existing service", async () => {
|
||||
// Create random directory to initialize
|
||||
const randomTmpDir = path.join(os.tmpdir(), uuid());
|
||||
fs.mkdirSync(randomTmpDir);
|
||||
describe("Adding a new service", () => {
|
||||
beforeAll(() => {
|
||||
mockFs({
|
||||
"maintainers.yml": MockFactory.createTestMaintainersYaml() as any
|
||||
});
|
||||
});
|
||||
|
||||
const maintainerFilePath = path.join(randomTmpDir, "maintainers.yaml");
|
||||
console.log(maintainerFilePath);
|
||||
// TODO: figure out this path for the file...
|
||||
// await cpFile(
|
||||
// process.cwd() + "/src/lib/maintainers.yaml",
|
||||
// maintainerFilePath
|
||||
// );
|
||||
// logger.info("File copied");
|
||||
afterAll(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
|
||||
// Create starting existing maintainers file.
|
||||
await writeSampleMaintainersFileToDir(maintainerFilePath);
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should update existing maintainers.yml with new service maintainers", async () => {
|
||||
const maintainersFilePath = "maintainers.yml";
|
||||
|
||||
const servicePath = "packages/my-new-service";
|
||||
const newUser = {
|
||||
email: "hello@example.com",
|
||||
name: "testUser"
|
||||
} as IUser;
|
||||
};
|
||||
|
||||
await addNewServiceToMaintainersFile(
|
||||
maintainerFilePath,
|
||||
"packages/my-new-service",
|
||||
[newUser]
|
||||
const writeSpy = jest.spyOn(fs, "writeFileSync");
|
||||
await addNewServiceToMaintainersFile(maintainersFilePath, servicePath, [
|
||||
newUser
|
||||
]);
|
||||
|
||||
const defaultMaintainersFileObject = MockFactory.createTestMaintainersYaml(
|
||||
false
|
||||
);
|
||||
|
||||
const actualUpdatedMaintainersFile = yaml.safeLoad(
|
||||
fs.readFileSync(maintainerFilePath, "utf8")
|
||||
) as IMaintainersFile;
|
||||
const expected: IMaintainersFile = {
|
||||
services: {
|
||||
...((defaultMaintainersFileObject as any) as IMaintainersFile).services,
|
||||
["./" + servicePath]: {
|
||||
maintainers: [newUser]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const expected = await yaml.safeLoad(`
|
||||
services:
|
||||
./:
|
||||
maintainers:
|
||||
- email: somegithubemailg@users.noreply.github.com
|
||||
name: my name
|
||||
./packages/service1:
|
||||
maintainers:
|
||||
- email: ""
|
||||
name: ""
|
||||
./packages/my-new-service:
|
||||
maintainers:
|
||||
- email: hello@example.com
|
||||
name: testUser
|
||||
`);
|
||||
|
||||
expect(actualUpdatedMaintainersFile).toEqual(expected);
|
||||
|
||||
// assert that this file is now updated with the new service..
|
||||
// 1. write to file
|
||||
// 1. read it back, yaml parse then compare to expected content
|
||||
expect(writeSpy).toBeCalledWith(
|
||||
maintainersFilePath,
|
||||
yaml.safeDump(expected),
|
||||
"utf8"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
const writeSampleMaintainersFileToDir = async (maintainersFilePath: string) => {
|
||||
await promisify(fs.writeFile)(
|
||||
maintainersFilePath,
|
||||
yaml.safeDump(
|
||||
yaml.safeLoad(`
|
||||
services:
|
||||
./:
|
||||
maintainers:
|
||||
- email: somegithubemailg@users.noreply.github.com
|
||||
name: my name
|
||||
./packages/service1:
|
||||
maintainers:
|
||||
- email: ""
|
||||
name: ""
|
||||
`)
|
||||
),
|
||||
"utf8"
|
||||
);
|
||||
};
|
||||
|
|
|
@ -119,7 +119,16 @@ const starterAzurePipelines = async (opts: {
|
|||
return yaml.safeDump(starter, { lineWidth: Number.MAX_SAFE_INTEGER });
|
||||
};
|
||||
|
||||
export const addNewServiceToMaintainersFile = async (
|
||||
/**
|
||||
* Update maintainers.yml with new service
|
||||
*
|
||||
* TODO: support for contributors(?)
|
||||
*
|
||||
* @param maintainersFilePath
|
||||
* @param newServicePath
|
||||
* @param serviceMaintainers
|
||||
*/
|
||||
export const addNewServiceToMaintainersFile = (
|
||||
maintainersFilePath: string,
|
||||
newServicePath: string,
|
||||
serviceMaintainers: IUser[]
|
||||
|
@ -132,11 +141,6 @@ export const addNewServiceToMaintainersFile = async (
|
|||
maintainers: serviceMaintainers
|
||||
};
|
||||
|
||||
// Write changes out to
|
||||
logger.info("updating maintainers.yaml");
|
||||
await promisify(fs.writeFile)(
|
||||
maintainersFilePath,
|
||||
yaml.safeDump(maintainersFile),
|
||||
"utf8"
|
||||
);
|
||||
logger.info("Updating maintainers.yaml");
|
||||
fs.writeFileSync(maintainersFilePath, yaml.safeDump(maintainersFile), "utf8");
|
||||
};
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
services:
|
||||
./:
|
||||
maintainers:
|
||||
- email: somegithubemailg@users.noreply.github.com
|
||||
name: my name
|
||||
./packages/service1:
|
||||
maintainers:
|
||||
- email: ""
|
||||
name: ""
|
||||
./packages/service2:
|
||||
maintainers:
|
||||
- email: ""
|
||||
name: ""
|
||||
contributors:
|
||||
- email: ""
|
||||
name: ""
|
||||
./packages/service3:
|
||||
maintainers:
|
||||
- email: ""
|
||||
name: ""
|
|
@ -0,0 +1,31 @@
|
|||
import yaml from "js-yaml";
|
||||
import { IMaintainersFile } from "../types";
|
||||
|
||||
export class MockFactory {
|
||||
public static createTestMaintainersYaml(
|
||||
asString = true
|
||||
): IMaintainersFile | string {
|
||||
const data: IMaintainersFile = {
|
||||
services: {
|
||||
"./": {
|
||||
maintainers: [
|
||||
{
|
||||
email: "somegithubemailg@users.noreply.github.com",
|
||||
name: "my name"
|
||||
}
|
||||
]
|
||||
},
|
||||
"./packages/service1": {
|
||||
maintainers: [
|
||||
{
|
||||
email: "hello@users.noreply.github.com",
|
||||
name: "testUser"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return asString ? yaml.dump(data) : data;
|
||||
}
|
||||
}
|
12
yarn.lock
12
yarn.lock
|
@ -438,6 +438,13 @@
|
|||
resolved "https://registry.yarnpkg.com/@types/node-emoji/-/node-emoji-1.8.1.tgz#689cb74fdf6e84309bcafce93a135dfecd01de3f"
|
||||
integrity sha512-0fRfA90FWm6KJfw6P9QGyo0HDTCmthZ7cWaBQndITlaWLTZ6njRyKwrwpzpg+n6kBXBIGKeUHEQuBx7bphGJkA==
|
||||
|
||||
"@types/mock-fs@^3.6.30":
|
||||
version "3.6.30"
|
||||
resolved "https://registry.yarnpkg.com/@types/mock-fs/-/mock-fs-3.6.30.tgz#4d812541e87b23577261a5aa95f704dd3d01e410"
|
||||
integrity sha1-TYElQeh7I1dyYaWqlfcE3T0B5BA=
|
||||
dependencies:
|
||||
"@types/node" "*"
|
||||
|
||||
"@types/node@*", "@types/node@^12.7.4":
|
||||
version "12.7.4"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.4.tgz#64db61e0359eb5a8d99b55e05c729f130a678b04"
|
||||
|
@ -4144,6 +4151,11 @@ mkdirp@0.x, mkdirp@^0.5.0, mkdirp@^0.5.1:
|
|||
dependencies:
|
||||
minimist "0.0.8"
|
||||
|
||||
mock-fs@^4.10.1:
|
||||
version "4.10.1"
|
||||
resolved "https://registry.yarnpkg.com/mock-fs/-/mock-fs-4.10.1.tgz#50a07a20114a6cdb119f35762f61f46266a1e323"
|
||||
integrity sha512-w22rOL5ZYu6HbUehB5deurghGM0hS/xBVyHMGKOuQctkk93J9z9VEOhDsiWrXOprVNQpP9uzGKdl8v9mFspKuw==
|
||||
|
||||
move-concurrently@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"
|
||||
|
|
Загрузка…
Ссылка в новой задаче