-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move dependency-graph and related logic to own module. Also add tests.
- Loading branch information
1 parent
d95c66e
commit 48e67b6
Showing
3 changed files
with
165 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ResolvePackumentVersionError } from "../packument-version-resolving"; | ||
import { DomainName } from "./domain-name"; | ||
import { SemanticVersion } from "./semantic-version"; | ||
import { RegistryUrl } from "./registry-url"; | ||
|
||
type ResolvedNode = Readonly<{ | ||
resolved: true; | ||
source: RegistryUrl | "built-in"; | ||
dependencies: Readonly<Record<DomainName, SemanticVersion>>; | ||
}>; | ||
type UnresolvedNode = Readonly<{ | ||
resolved: false; | ||
error: ResolvePackumentVersionError; | ||
}>; | ||
type GraphNode = ResolvedNode | UnresolvedNode; | ||
|
||
/** | ||
* A graph indicating which packages depend on which others. | ||
* Dependencies are keyed first by their name and then version. | ||
* @example | ||
* { | ||
* "com.some.package": { | ||
* "1.0.0": { | ||
* resolved: true, | ||
* source: "https://package.openupm.com", | ||
* dependencies: { "com.other.package": "2.0.0" } | ||
* } | ||
* }, | ||
* "com.other.package": { | ||
* "2.0.0": { | ||
* resolved: true, | ||
* source: "https://package.openupm.com", | ||
* dependencies: { } | ||
* } | ||
* } | ||
* } | ||
*/ | ||
export type DependencyGraph = Readonly< | ||
Record<DomainName, Readonly<Record<SemanticVersion, GraphNode>>> | ||
>; | ||
|
||
/** | ||
* An empty dependency graph. | ||
*/ | ||
export const emptyDependencyGraph: DependencyGraph = {}; | ||
|
||
/** | ||
* Add or replace a node in the graph. | ||
* @param graph The graph. | ||
* @param packageName The key package name. | ||
* @param version The key version. | ||
* @param node The new node. | ||
* @returns The updated graph. | ||
*/ | ||
export function setGraphNode( | ||
graph: DependencyGraph, | ||
packageName: DomainName, | ||
version: SemanticVersion, | ||
node: GraphNode | ||
): DependencyGraph { | ||
return { | ||
...graph, | ||
[packageName]: { ...graph[packageName], [version]: node }, | ||
}; | ||
} | ||
|
||
/** | ||
* Checks whether a graph has a node at the given key. | ||
* @param graph The graph. | ||
* @param packageName The key package name. | ||
* @param version The key version. | ||
*/ | ||
export function graphHasNodeAt( | ||
graph: DependencyGraph, | ||
packageName: DomainName, | ||
version: SemanticVersion | ||
): boolean { | ||
return graph[packageName]?.[version] !== undefined; | ||
} |
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,78 @@ | ||
import { | ||
emptyDependencyGraph, | ||
graphHasNodeAt, | ||
setGraphNode, | ||
} from "../../src/domain/dependency-graph"; | ||
import { makeDomainName } from "../../src/domain/domain-name"; | ||
import { makeSemanticVersion } from "../../src/domain/semantic-version"; | ||
import { exampleRegistryUrl } from "./data-registry"; | ||
import { PackumentNotFoundError } from "../../src/common-errors"; | ||
|
||
describe("dependency graph", () => { | ||
const somePackage = makeDomainName("com.some.package"); | ||
const someVersion = makeSemanticVersion("1.0.0"); | ||
|
||
describe("set node", () => { | ||
it("should add new node", () => { | ||
const initial = emptyDependencyGraph; | ||
const node = { | ||
resolved: true, | ||
source: exampleRegistryUrl, | ||
dependencies: {}, | ||
} as const; | ||
|
||
const withNode = setGraphNode(initial, somePackage, someVersion, node); | ||
|
||
expect(withNode).toEqual({ [somePackage]: { [someVersion]: node } }); | ||
}); | ||
|
||
it("should overwrite node", () => { | ||
const initial = setGraphNode( | ||
emptyDependencyGraph, | ||
somePackage, | ||
someVersion, | ||
{ | ||
resolved: false, | ||
error: new PackumentNotFoundError(somePackage), | ||
} | ||
); | ||
const node = { | ||
resolved: true, | ||
source: exampleRegistryUrl, | ||
dependencies: {}, | ||
} as const; | ||
|
||
const withNode = setGraphNode(initial, somePackage, someVersion, node); | ||
|
||
expect(withNode).toEqual({ [somePackage]: { [someVersion]: node } }); | ||
}); | ||
}); | ||
|
||
describe("has node", () => { | ||
it("should be false if node was not added", () => { | ||
const actual = graphHasNodeAt( | ||
emptyDependencyGraph, | ||
somePackage, | ||
someVersion | ||
); | ||
|
||
expect(actual).toBeFalsy(); | ||
}); | ||
|
||
it("should be true if node was added", () => { | ||
const initial = setGraphNode( | ||
emptyDependencyGraph, | ||
somePackage, | ||
someVersion, | ||
{ | ||
resolved: false, | ||
error: new PackumentNotFoundError(somePackage), | ||
} | ||
); | ||
|
||
const actual = graphHasNodeAt(initial, somePackage, someVersion); | ||
|
||
expect(actual).toBeTruthy(); | ||
}); | ||
}); | ||
}); |