Skip to content

Commit

Permalink
refactor: ensure CollieRepository always uses an absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesRudolph committed Aug 29, 2023
1 parent 7931411 commit ad4790a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/commands/init.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function registerInitCommand(program: TopLevelCommand) {

// we would like to create the CollieRepository after git has been initialized
// but we need it to build git
const kit = new CollieRepository(directory);
const kit = CollieRepository.uninitialized(directory);
const logger = new Logger(kit, opts);

// ensure git is initialized
Expand Down
2 changes: 1 addition & 1 deletion src/commands/kit/import.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function registerImportCmd(program: TopLevelCommand) {

async function promptForKitModuleId(logger: Logger, hubRepoDir: string) {
// note: the hub is a standard collie repository for the most part, so we can just parse it with the same code
const repo = new CollieRepository(hubRepoDir);
const repo = await CollieRepository.load(hubRepoDir);
const validator = new ModelValidator(logger);

const moduleRepo = await KitModuleRepository.load(repo, validator, logger);
Expand Down
11 changes: 9 additions & 2 deletions src/model/CollieRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { assertEquals } from "std/testing/assert";
import { assertEquals, assertNotEquals } from "std/testing/assert";
import { CollieRepository } from "./CollieRepository.ts";

Deno.test("relativePath calculates paths relative to repository root", () => {
const sut = new CollieRepository("/tmp/test");
const sut = CollieRepository.uninitialized("/tmp/test");

const result = sut.relativePath("/tmp/test/123");
assertEquals(result, "123");
});

Deno.test("relativePath does not work with paths that are already relative", () => {
const sut = CollieRepository.uninitialized("/tmp/test");

const result = sut.relativePath("123");
assertNotEquals(result, "123");
});
19 changes: 17 additions & 2 deletions src/model/CollieRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import * as fs from "std/fs";
import * as path from "std/path";

export class CollieRepository {
constructor(private readonly repoDir: string) {}
private constructor(private readonly repoDir: string) {
if (!path.isAbsolute(repoDir)) {
throw new Error(
`Tried to load CollieRepository with an invalid path. '${repoDir}' is not an absolute path.`,
);
}
}

/**
* Resolve a path relative to the kit repository
Expand All @@ -11,7 +17,10 @@ export class CollieRepository {
return path.resolve(this.repoDir, ...pathSegments);
}

/** */
/**
* Calculate a relative path between the repoDir
* @param toPath an absolute path
*/
relativePath(toPath: string) {
return path.relative(this.repoDir, toPath);
}
Expand Down Expand Up @@ -55,4 +64,10 @@ export class CollieRepository {

return new CollieRepository(absolutePath);
}

static uninitialized(dir: string) {
const absolutePath = path.resolve(dir);

return new CollieRepository(absolutePath);
}
}
2 changes: 1 addition & 1 deletion src/model/schemas/ModelValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ModelValidator } from "./ModelValidator.ts";
import { assertEquals } from "std/testing/assert";

Deno.test("can validate FoundationConfig", () => {
const collie = new CollieRepository("./");
const collie = CollieRepository.uninitialized("./");
const logger = new Logger(collie, {
debug: false,
verbose: false,
Expand Down

0 comments on commit ad4790a

Please sign in to comment.