From d21ad1312b6c6ba1252d29c892b85347f37f88d3 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Thu, 10 May 2018 14:02:19 -0700 Subject: [PATCH] Add `git/render.js` with `nodeDescription` (#262) Test Plan: Unit tests added, with full coverage of reachable cases. wchargin-branch: git-render --- src/plugins/git/render.js | 30 +++++++++++++++ src/plugins/git/render.test.js | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 src/plugins/git/render.js create mode 100644 src/plugins/git/render.test.js diff --git a/src/plugins/git/render.js b/src/plugins/git/render.js new file mode 100644 index 0000000..d809b44 --- /dev/null +++ b/src/plugins/git/render.js @@ -0,0 +1,30 @@ +// @flow + +import type {Address} from "../../core/address"; +import type {Graph} from "../../core/graph"; +import type {NodeType, SubmoduleCommitPayload} from "./types"; + +/** + * Describe a node provided by the Git plugin. + */ +export function nodeDescription(graph: Graph, address: Address) { + const type: NodeType = (address.type: any); + switch (type) { + case "COMMIT": + return `commit ${address.id}`; + case "TREE": + return `tree ${address.id}`; + case "BLOB": + return `blob ${address.id}`; + case "SUBMODULE_COMMIT": { + const payload: SubmoduleCommitPayload = graph.node(address).payload; + return `submodule commit ${payload.hash} in ${payload.url}`; + } + case "TREE_ENTRY": + return `entry ${address.id}`; + default: + // eslint-disable-next-line no-unused-expressions + (type: empty); + throw new Error(`unknown type: ${type}`); + } +} diff --git a/src/plugins/git/render.test.js b/src/plugins/git/render.test.js new file mode 100644 index 0000000..5c6458a --- /dev/null +++ b/src/plugins/git/render.test.js @@ -0,0 +1,70 @@ +// @flow + +import type { + CommitNodePayload, + BlobNodePayload, + TreeNodePayload, + TreeEntryNodePayload, + SubmoduleCommitPayload, +} from "./types"; +import type {Node} from "../../core/graph"; +import {Graph} from "../../core/graph"; +import {_makeAddress, commitAddress} from "./address"; +import {nodeDescription} from "./render"; +import {submoduleCommitId, treeEntryId} from "./types"; + +describe("nodeDescription", () => { + it("describes commits", () => { + const node: Node = { + address: commitAddress("cafebabe"), + payload: (({}: any): {||}), + }; + expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual( + "commit cafebabe" + ); + }); + + it("describes trees", () => { + const node: Node = { + address: _makeAddress("TREE", "deadbeef"), + payload: (({}: any): {||}), + }; + expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual( + "tree deadbeef" + ); + }); + + it("describes blobs", () => { + const node: Node = { + address: _makeAddress("BLOB", "01010101"), + payload: (({}: any): {||}), + }; + expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual( + "blob 01010101" + ); + }); + + it("describes submodule commits", () => { + const hash = "15615651"; + const url = "https://github.com/sourcecred/example-git-submodule.git"; + const node: Node = { + address: _makeAddress("SUBMODULE_COMMIT", submoduleCommitId(hash, url)), + payload: {hash, url}, + }; + expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual( + `submodule commit ${hash} in ${url}` + ); + }); + + it("describes tree entries", () => { + const tree = "76476476323"; + const name = "healing"; + const node: Node = { + address: _makeAddress("TREE_ENTRY", treeEntryId(tree, name)), + payload: {name}, + }; + expect(nodeDescription(new Graph().addNode(node), node.address)).toEqual( + `entry ${tree}:${name}` + ); + }); +});