Skip to content

Commit d74e7af

Browse files
committed
test(gatsby-adapter-netlify): add unit tests for entries injection
1 parent 743cb95 commit d74e7af

File tree

2 files changed

+162
-10
lines changed

2 files changed

+162
-10
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import fs from "fs-extra"
2+
import { tmpdir } from "os"
3+
import { join } from "path"
4+
import {
5+
injectEntries,
6+
ADAPTER_MARKER_START,
7+
ADAPTER_MARKER_END,
8+
NETLIFY_PLUGIN_MARKER_START,
9+
NETLIFY_PLUGIN_MARKER_END,
10+
GATSBY_PLUGIN_MARKER_START,
11+
} from "../route-handler"
12+
13+
function generateLotOfContent(placeholderCharacter: string): string {
14+
return (placeholderCharacter.repeat(80) + `\n`).repeat(1_000_000)
15+
}
16+
17+
const newAdapterContent = generateLotOfContent(`a`)
18+
const previousAdapterContent =
19+
ADAPTER_MARKER_START +
20+
`\n` +
21+
generateLotOfContent(`b`) +
22+
ADAPTER_MARKER_END +
23+
`\n`
24+
25+
const gatsbyPluginNetlifyContent =
26+
GATSBY_PLUGIN_MARKER_START + `\n` + generateLotOfContent(`c`)
27+
28+
const netlifyPluginGatsbyContent =
29+
NETLIFY_PLUGIN_MARKER_START +
30+
`\n` +
31+
generateLotOfContent(`c`) +
32+
NETLIFY_PLUGIN_MARKER_END +
33+
`\n`
34+
35+
const customContent1 =
36+
`# customContent1 start` +
37+
`\n` +
38+
generateLotOfContent(`x`) +
39+
`# customContent1 end` +
40+
`\n`
41+
const customContent2 =
42+
`# customContent2 start` +
43+
`\n` +
44+
generateLotOfContent(`y`) +
45+
`# customContent2 end` +
46+
`\n`
47+
const customContent3 =
48+
`# customContent3 start` +
49+
`\n` +
50+
generateLotOfContent(`z`) +
51+
`# customContent3 end` +
52+
`\n`
53+
54+
async function getContent(previousContent?: string): Promise<string> {
55+
const filePath = join(
56+
await fs.mkdtemp(join(tmpdir(), `inject-entries`)),
57+
`out.txt`
58+
)
59+
60+
if (typeof previousContent !== `undefined`) {
61+
await fs.writeFile(filePath, previousContent)
62+
}
63+
64+
await injectEntries(filePath, newAdapterContent)
65+
66+
return fs.readFile(filePath, `utf8`)
67+
}
68+
69+
describe(`route-handler`, () => {
70+
describe(`injectEntries`, () => {
71+
it(`no cached file`, async () => {
72+
const content = await getContent()
73+
74+
expect(content.indexOf(newAdapterContent)).not.toBe(-1)
75+
})
76+
77+
describe(`has cached file`, () => {
78+
it(`no previous adapter or plugins or custom entries`, async () => {
79+
const content = await getContent(``)
80+
81+
expect(content.indexOf(newAdapterContent)).not.toBe(-1)
82+
})
83+
84+
it(`has just custom entries`, async () => {
85+
const content = await getContent(customContent1)
86+
87+
expect(content.indexOf(newAdapterContent)).not.toBe(-1)
88+
expect(content.indexOf(customContent1)).not.toBe(-1)
89+
})
90+
91+
it(`has just gatsby-plugin-netlify entries`, async () => {
92+
const content = await getContent(gatsbyPluginNetlifyContent)
93+
94+
expect(content.indexOf(newAdapterContent)).not.toBe(-1)
95+
// it removes gatsby-plugin-netlify entries
96+
expect(content.indexOf(GATSBY_PLUGIN_MARKER_START)).toBe(-1)
97+
expect(content.indexOf(gatsbyPluginNetlifyContent)).toBe(-1)
98+
})
99+
100+
it(`has just netlify-plugin-gatsby entries`, async () => {
101+
const content = await getContent(netlifyPluginGatsbyContent)
102+
103+
expect(content.indexOf(newAdapterContent)).not.toBe(-1)
104+
// it removes netlify-plugin-gatsby entries
105+
expect(content.indexOf(NETLIFY_PLUGIN_MARKER_START)).toBe(-1)
106+
expect(content.indexOf(NETLIFY_PLUGIN_MARKER_END)).toBe(-1)
107+
expect(content.indexOf(netlifyPluginGatsbyContent)).toBe(-1)
108+
})
109+
110+
it(`has gatsby-plugin-netlify, nelify-plugin-gatsby, custom content and previous adapter content`, async () => {
111+
// kitchen-sink
112+
const previousContent =
113+
customContent1 +
114+
previousAdapterContent +
115+
customContent2 +
116+
netlifyPluginGatsbyContent +
117+
customContent3 +
118+
gatsbyPluginNetlifyContent
119+
120+
const content = await getContent(previousContent)
121+
122+
expect(content.indexOf(newAdapterContent)).not.toBe(-1)
123+
124+
// it preserve any custom entries
125+
expect(content.indexOf(customContent1)).not.toBe(-1)
126+
expect(content.indexOf(customContent2)).not.toBe(-1)
127+
expect(content.indexOf(customContent3)).not.toBe(-1)
128+
129+
// it removes previous gatsby-adapter-netlify entries
130+
expect(content.indexOf(previousAdapterContent)).toBe(-1)
131+
132+
// it removes gatsby-plugin-netlify entries
133+
expect(content.indexOf(GATSBY_PLUGIN_MARKER_START)).toBe(-1)
134+
expect(content.indexOf(gatsbyPluginNetlifyContent)).toBe(-1)
135+
136+
// it removes netlify-plugin-gatsby entries
137+
expect(content.indexOf(NETLIFY_PLUGIN_MARKER_START)).toBe(-1)
138+
expect(content.indexOf(NETLIFY_PLUGIN_MARKER_END)).toBe(-1)
139+
expect(content.indexOf(netlifyPluginGatsbyContent)).toBe(-1)
140+
})
141+
})
142+
})
143+
})

packages/gatsby-adapter-netlify/src/route-handler.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,42 @@ const toNetlifyPath = (fromPath: string, toPath: string): Array<string> => {
2020

2121
return [netlifyFromPath, netlifyToPath]
2222
}
23-
const MARKER_START = `# gatsby-adapter-netlify start`
24-
const MARKER_END = `# gatsby-adapter-netlify end`
25-
26-
async function injectEntries(fileName: string, content: string): Promise<void> {
23+
export const ADAPTER_MARKER_START = `# gatsby-adapter-netlify start`
24+
export const ADAPTER_MARKER_END = `# gatsby-adapter-netlify end`
25+
export const NETLIFY_PLUGIN_MARKER_START = `# @netlify/plugin-gatsby redirects start`
26+
export const NETLIFY_PLUGIN_MARKER_END = `# @netlify/plugin-gatsby redirects end`
27+
export const GATSBY_PLUGIN_MARKER_START = `## Created with gatsby-plugin-netlify`
28+
29+
export async function injectEntries(
30+
fileName: string,
31+
content: string
32+
): Promise<void> {
2733
await fs.ensureFile(fileName)
2834

2935
const data = await fs.readFile(fileName, `utf8`)
30-
const [initial = ``, rest = ``] = data.split(MARKER_START)
31-
const [, final = ``] = rest.split(MARKER_END)
36+
const [initial = ``, rest = ``] = data.split(ADAPTER_MARKER_START)
37+
const [, final = ``] = rest.split(ADAPTER_MARKER_END)
3238
const out = [
3339
initial === EOL ? `` : initial,
3440
initial.endsWith(EOL) ? `` : EOL,
35-
MARKER_START,
41+
ADAPTER_MARKER_START,
3642
EOL,
3743
content,
3844
EOL,
39-
MARKER_END,
45+
ADAPTER_MARKER_END,
4046
final.startsWith(EOL) ? `` : EOL,
4147
final === EOL ? `` : final,
4248
]
4349
.filter(Boolean)
4450
.join(``)
4551
.replace(
46-
/# @netlify\/plugin-gatsby redirects start(.|\n|\r)*# @netlify\/plugin-gatsby redirects end/gm,
52+
new RegExp(
53+
`${NETLIFY_PLUGIN_MARKER_START}(.|\n|\r)*${NETLIFY_PLUGIN_MARKER_END}`,
54+
`gm`
55+
),
4756
``
4857
)
49-
.replace(/## Created with gatsby-plugin-netlify(.|\n|\r)*$/gm, ``)
58+
.replace(new RegExp(`${GATSBY_PLUGIN_MARKER_START}(.|\n|\r)*$`, `gm`), ``)
5059

5160
await fs.outputFile(fileName, out)
5261
}

0 commit comments

Comments
 (0)