Skip to content

Commit 89ff08d

Browse files
authored
Fix CI breaking on new release (#770)
* remove this commit: Fake version to break CI * refactor: Extract tests/utils function to execute CLI commands * fix: Make init 'From contract' (scaffold) tests work The 'From contract' `init` tests would fail whenever the `package.json`'s version gets updated. That's because `init` would try to install the version generated from the `scaffold` base `package.json` (current in branch). To fix this we'll be using `npm link` from now on **only** for the tests. 1. We do a `npm link` on `graph-cli` itself (root of the repo); 2. Then we run `npm link @graphprotocol/graph-cli` on each `from-contract` folder. We also removed the `@graphprotocol/graph-cli` from the `scaffold` if there's a `GRAPH_CLI_TESTS` env var set. * fix: Make init 'From example' tests work Similar principle from the 'From contract' tests fix, however for this one we **also** needed to modify the cloned repo's `package.json` to remove the `graph-cli` dependency, and add it via `npm link` (setup from previous commit). * remove this commit: Revert fake version that broke CI * refactor: Move GRAPH_CLI_TESTS env var to jest setup This is so that you don't have to set this env var manually when running the tests locally.
1 parent 40149b1 commit 89ff08d

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ module.exports = {
4949
// forceCoverageMatch: [],
5050

5151
// A path to a module which exports an async function that is triggered once before all test suites
52-
// globalSetup: null,
52+
globalSetup: './tests/cli/globalSetup',
5353

5454
// A path to a module which exports an async function that is triggered once after all test suites
55-
// globalTeardown: null,
55+
globalTeardown: './tests/cli/globalTeardown',
5656

5757
// A set of global variables that need to be available in all test environments
5858
// globals: {},

src/commands/init.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,26 @@ const initRepository = async (toolbox, directory) =>
538538
},
539539
)
540540

541+
// Only used for local testing / continuous integration.
542+
//
543+
// This requires that the command `npm link` is called
544+
// on the root directory of this repository, as described here:
545+
// https://docs.npmjs.com/cli/v7/commands/npm-link.
546+
const npmLinkToLocalCli = async (toolbox, directory) => {
547+
if (process.env.GRAPH_CLI_TESTS) {
548+
await toolbox.system.run('npm link @graphprotocol/graph-cli', { cwd: directory })
549+
}
550+
}
551+
541552
const installDependencies = async (toolbox, directory, installCommand) =>
542553
await withSpinner(
543554
`Install dependencies with ${toolbox.print.colors.muted(installCommand)}`,
544555
`Failed to install dependencies`,
545556
`Warnings while installing dependencies`,
546557
async spinner => {
558+
// Links to local graph-cli if we're running the automated tests
559+
await npmLinkToLocalCli(toolbox, directory)
560+
547561
await toolbox.system.run(installCommand, { cwd: directory })
548562
return true
549563
},
@@ -651,6 +665,11 @@ const initSubgraphFromExample = async (
651665
delete pkgJson['license']
652666
delete pkgJson['repository']
653667

668+
// Remove example's cli in favor of the local one (added via `npm link`)
669+
if (process.env.GRAPH_CLI_TESTS) {
670+
delete pkgJson['devDependencies']['@graphprotocol/graph-cli']
671+
}
672+
654673
// Write package.json
655674
await filesystem.write(pkgJsonFilename, pkgJson, { jsonIndent: 2 })
656675
return true

src/scaffold.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const abiEvents = abi =>
1818
setName: (event, name) => event.set('_alias', name),
1919
})
2020

21+
const graphCliVersion = process.env.GRAPH_CLI_TESTS
22+
// JSON.stringify should remove this key, we will install the local
23+
// graph-cli for the tests using `npm link` instead of fetching from npm.
24+
? undefined
25+
// For scaffolding real subgraphs
26+
: `${module.exports.version}`
27+
2128
// package.json
2229

2330
const generatePackageJson = ({ subgraphName, node }) =>
@@ -41,7 +48,7 @@ const generatePackageJson = ({ subgraphName, node }) =>
4148
subgraphName,
4249
},
4350
dependencies: {
44-
'@graphprotocol/graph-cli': `${module.exports.version}`,
51+
'@graphprotocol/graph-cli': graphCliVersion,
4552
'@graphprotocol/graph-ts': `0.22.1`,
4653
},
4754
}),

tests/cli/globalSetup.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { npmLinkCli } = require('./util')
2+
3+
module.exports = async () => {
4+
process.env.GRAPH_CLI_TESTS = '1'
5+
await npmLinkCli()
6+
}

tests/cli/globalTeardown.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { npmUnlinkCli } = require('./util')
2+
3+
module.exports = async () => {
4+
delete process.env.GRAPH_CLI_TESTS
5+
await npmUnlinkCli()
6+
}

tests/cli/init.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs-extra')
22
const path = require('path')
3-
const cliTest = require('./util').cliTest
3+
const { cliTest } = require('./util')
44

55
describe('Init', () => {
66
let baseDir = path.join(__dirname, 'init')

tests/cli/util.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const cliTest = (title, args, testPath, options) => {
1313
let cwd =
1414
options !== undefined && options.cwd ? options.cwd : resolvePath(`./${testPath}`)
1515

16-
let [exitCode, stdout, stderr] = await runCli(args, cwd)
16+
let [exitCode, stdout, stderr] = await runGraphCli(args, cwd)
1717

1818
let expectedExitCode = undefined
1919
if (options !== undefined && options.exitCode !== undefined) {
@@ -55,18 +55,14 @@ const cliTest = (title, args, testPath, options) => {
5555
)
5656
}
5757

58-
const runCli = async (args = [], cwd = process.cwd()) => {
59-
// Resolve the path to graph.js
60-
let graphCli = path.join(__dirname, '..', '..', 'bin', 'graph')
61-
58+
const runCommand = async (command, args = [], cwd = process.cwd()) => {
6259
// Make sure to set an absolute working directory
6360
cwd = cwd[0] !== '/' ? path.resolve(__dirname, cwd) : cwd
6461

6562
return new Promise((resolve, reject) => {
6663
let stdout = ''
6764
let stderr = ''
68-
const command = `${graphCli} ${args.join(' ')}`
69-
const child = spawn(command, { cwd })
65+
const child = spawn(`${command} ${args.join(' ')}`, { cwd })
7066

7167
child.on('error', error => {
7268
reject(error)
@@ -86,7 +82,20 @@ const runCli = async (args = [], cwd = process.cwd()) => {
8682
})
8783
}
8884

85+
const runGraphCli = async (args, cwd) => {
86+
// Resolve the path to graph.js
87+
let graphCli = path.join(__dirname, '..', '..', 'bin', 'graph')
88+
89+
return await runCommand(graphCli, args, cwd)
90+
}
91+
92+
const npmLinkCli = () => runCommand('npm', ['link'])
93+
94+
const npmUnlinkCli = () => runCommand('npm', ['unlink'])
95+
8996
module.exports = {
9097
cliTest,
91-
runCli,
98+
npmLinkCli,
99+
npmUnlinkCli,
100+
runGraphCli,
92101
}

0 commit comments

Comments
 (0)