Extract code to create the example repository (#152)

Test Plan:
Note that the snapshot change is simply a move: no SHAs were changed.

wchargin-branch: extract-example-repository-code
This commit is contained in:
William Chargin 2018-04-26 19:38:29 -07:00 committed by GitHub
parent 6f9941b526
commit 75fd068a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 52 deletions

View File

@ -133,12 +133,3 @@ Object {
},
}
`;
exports[`we create a deterministic repository 1`] = `
Array [
"301749e9af8cd6e9aee3a49a64029b98a4695e34",
"cbb26b570d1eed3c681b8f03ff31231c1bffd6d6",
"4be43f1cda04e51e42fec0cfe8e1e2dff116e839",
"677b340674bde17fdaac3b5f5eef929139ef2a52",
]
`;

View File

@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`createExampleRepo is deterministic 1`] = `
Array [
"301749e9af8cd6e9aee3a49a64029b98a4695e34",
"cbb26b570d1eed3c681b8f03ff31231c1bffd6d6",
"4be43f1cda04e51e42fec0cfe8e1e2dff116e839",
"677b340674bde17fdaac3b5f5eef929139ef2a52",
]
`;

View File

@ -0,0 +1,46 @@
// @flow
import mkdirp from "mkdirp";
import {makeUtils} from "../gitUtils";
import type {Hash} from "../types";
type RepositoryInfo = {|
+path: string,
+commits: $ReadOnlyArray<Hash>, // in oldest-to-newest order
|};
export function createExampleRepo(intoDirectory: string): RepositoryInfo {
const repositoryPath = intoDirectory;
mkdirp(repositoryPath);
const git = makeUtils(repositoryPath);
const commits = [];
git.exec(["init"]);
git.writeAndStage("README.txt", "Amazing physics going on...\n");
git.deterministicCommit("Initial commit");
commits.push(git.head());
git.writeAndStage("src/index.py", "import antigravity\n");
git.writeAndStage(
"src/quantum_gravity.py",
'raise NotImplementedError("TODO(physicists)")\n'
);
git.writeAndStage("TODOS.txt", "1. Resolve quantum gravity\n");
git.deterministicCommit("Discover gravity");
commits.push(git.head());
git.writeAndStage(
"src/quantum_gravity.py",
"import random\nif random.random() < 0.5:\n import antigravity\n"
);
git.deterministicCommit("Solve quantum gravity");
commits.push(git.head());
git.exec(["rm", "TODOS.txt"]);
git.deterministicCommit("Clean up TODOS");
commits.push(git.head());
return {path: repositoryPath, commits};
}

View File

@ -0,0 +1,24 @@
// @flow
import tmp from "tmp";
import {createExampleRepo} from "./exampleRepo";
const cleanups: (() => void)[] = [];
afterAll(() => {
cleanups.forEach((f) => {
f();
});
});
function mkdtemp() {
const result = tmp.dirSync({unsafeCleanup: true});
cleanups.push(() => result.removeCallback());
return result.name;
}
describe("createExampleRepo", () => {
it("is deterministic", () => {
expect(createExampleRepo(mkdtemp()).commits).toMatchSnapshot();
});
});

View File

@ -2,6 +2,7 @@
import tmp from "tmp";
import {createExampleRepo} from "./demoData/exampleRepo";
import {makeUtils} from "./gitUtils";
import {loadRepository} from "./loadRepository";
@ -18,54 +19,14 @@ function mkdtemp() {
return result.name;
}
function createRepository(): {path: string, commits: string[]} {
const repositoryPath = mkdtemp();
const git = makeUtils(repositoryPath);
git.exec(["init"]);
git.writeAndStage("README.txt", "Amazing physics going on...\n");
git.deterministicCommit("Initial commit");
const commit1 = git.head();
git.writeAndStage("src/index.py", "import antigravity\n");
git.writeAndStage(
"src/quantum_gravity.py",
'raise NotImplementedError("TODO(physicists)")\n'
);
git.writeAndStage("TODOS.txt", "1. Resolve quantum gravity\n");
git.deterministicCommit("Discover gravity");
const commit2 = git.head();
git.writeAndStage(
"src/quantum_gravity.py",
"import random\nif random.random() < 0.5:\n import antigravity\n"
);
git.deterministicCommit("Solve quantum gravity");
const commit3 = git.head();
git.exec(["rm", "TODOS.txt"]);
git.deterministicCommit("Clean up TODOS");
const commit4 = git.head();
return {
path: repositoryPath,
commits: [commit1, commit2, commit3, commit4],
};
}
test("we create a deterministic repository", () => {
expect(createRepository().commits).toMatchSnapshot();
});
describe("loadRepository", () => {
it("loads from HEAD", () => {
const repository = createRepository();
const repository = createExampleRepo(mkdtemp());
expect(loadRepository(repository.path, "HEAD")).toMatchSnapshot();
});
it("processes an old commit", () => {
const repository = createRepository();
const repository = createExampleRepo(mkdtemp());
const whole = loadRepository(repository.path, "HEAD");
const part = loadRepository(repository.path, repository.commits[1]);
@ -88,7 +49,7 @@ describe("loadRepository", () => {
const repositoryPath = mkdtemp();
const git = makeUtils(repositoryPath);
const subproject = createRepository();
const subproject = createExampleRepo(mkdtemp());
git.exec(["init"]);
git.exec(["submodule", "--quiet", "add", subproject.path, "physics"]);