Skip to content

Commit

Permalink
don't introduce a breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Apr 1, 2019
1 parent 8b10d38 commit 75ad8cf
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/gatsby-transformer-toml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"bluebird": "^3.5.0",
"toml": "^2.3.2"
},
"devDependencies": {
Expand All @@ -25,7 +24,7 @@
],
"license": "MIT",
"peerDependencies": {
"gatsby": "^2.0.15"
"gatsby": "^2.0.0"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-toml",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,107 @@ Array [
],
]
`;

exports[`Process TOML nodes correctly fallsback to crypto when createDigestContent is not available 1`] = `
Array [
Array [
Object {
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest fallback",
"type": "",
},
"parent": "whatever",
"the": Object {
"hard": Object {
"another_test_string": " Same thing, but with a string #",
"bit#": Object {
"multi_line_array": Array [
"]",
],
"what?": "You don't think some user won't do that?",
},
"harder_test_string": " And when \\"'s are in the string, along with # \\"",
"test_array": Array [
"] ",
" # ",
],
"test_array2": Array [
"Test #11 ]proved that",
"Experiment #9 was a success",
],
},
"test_string": "You'll hate me after this - #",
},
},
],
]
`;

exports[`Process TOML nodes correctly fallsback to crypto when createDigestContent is not available 2`] = `
Array [
Array [
Object {
"child": Object {
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "contentDigest fallback",
"type": "",
},
"parent": "whatever",
"the": Object {
"hard": Object {
"another_test_string": " Same thing, but with a string #",
"bit#": Object {
"multi_line_array": Array [
"]",
],
"what?": "You don't think some user won't do that?",
},
"harder_test_string": " And when \\"'s are in the string, along with # \\"",
"test_array": Array [
"] ",
" # ",
],
"test_array2": Array [
"Test #11 ]proved that",
"Experiment #9 was a success",
],
},
"test_string": "You'll hate me after this - #",
},
},
"parent": Object {
"children": Array [],
"content": "
[the]
test_string = \\"You'll hate me after this - #\\"
[the.hard]
test_array = [ \\"] \\", \\" # \\"] # ]
test_array2 = [ \\"Test #11 ]proved that\\", \\"Experiment #9 was a success\\" ]
another_test_string = \\" Same thing, but with a string #\\"
harder_test_string = \\" And when /\\"'s are in the string, along with # /\\"\\"
# Things will get harder
[the.hard.\\"bit#\\"]
\\"what?\\" = \\"You don't think some user won't do that?\\"
multi_line_array = [
\\"]\\",
# ] Oh yes I did
]
",
"extension": "toml",
"id": "whatever",
"internal": Object {
"contentDigest": "whatever",
},
"name": "test",
"parent": null,
},
},
],
]
`;
48 changes: 48 additions & 0 deletions packages/gatsby-transformer-toml/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,61 @@ describe(`Process TOML nodes correctly`, () => {
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createDigestContent = jest.fn().mockReturnValue(`contentDigest`)
jest.mock(`../create-content-digest`)
const createDigestContentFallback = require(`../create-content-digest`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
createDigestContent,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(1)
expect(createParentChildLink).toHaveBeenCalledTimes(1)
expect(createDigestContent).toHaveBeenCalledTimes(1)
expect(createDigestContentFallback).toHaveBeenCalledTimes(0)
})
})

it(`fallsback to crypto when createDigestContent is not available`, async () => {
// Unfortunately due to TOML limitations no JSON -> TOML convertors exist,
// which means that we are stuck with JS template literals.
node.content = `
[the]
test_string = "You'll hate me after this - #"
[the.hard]
test_array = [ "] ", " # "] # ]
test_array2 = [ "Test #11 ]proved that", "Experiment #9 was a success" ]
another_test_string = " Same thing, but with a string #"
harder_test_string = " And when \\"'s are in the string, along with # \\""
# Things will get harder
[the.hard."bit#"]
"what?" = "You don't think some user won't do that?"
multi_line_array = [
"]",
# ] Oh yes I did
]
`

const createNode = jest.fn()
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
jest.mock(`../create-content-digest`)
const createDigestContent = require(`../create-content-digest`)
createDigestContent.mockImplementation(() => `contentDigest fallback`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby-transformer-toml/src/create-content-digest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// TODO remove in gatsby v3
// keeps old behaviour in tact when createDigestContent is not available
const crypto = require(`crypto`)

module.exports = content =>
crypto
.createHash(`md5`)
.update(content)
.digest(`hex`)
4 changes: 3 additions & 1 deletion packages/gatsby-transformer-toml/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async function onCreateNode({
// 1) More TOML files -> more types
// 2) Different files with the same name creating conflicts
const parsedContentStr = JSON.stringify(parsedContent)
const contentDigest = createDigestContent(parsedContentStr)
const contentDigest = createDigestContent
? createDigestContent(parsedContentStr)
: require(`./create-content-digest`)(parsedContentStr)

const newNode = {
...parsedContent,
Expand Down

0 comments on commit 75ad8cf

Please sign in to comment.