Skip to content

Commit

Permalink
Cache package lock when running tests to speed up installation (#44520)
Browse files Browse the repository at this point in the history
  • Loading branch information
jankaifer authored Jan 3, 2023
1 parent b6530bb commit a7b046d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .github/actions/next-stats-action/src/prepare/repo-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module.exports = (actionInfo) => {
pkgs = await fs.readdir(path.join(repoDir, 'packages'))
} catch (err) {
if (err.code === 'ENOENT') {
console.log('no packages to link')
require('console').log('no packages to link')
return pkgPaths
}
throw err
Expand All @@ -105,7 +105,7 @@ module.exports = (actionInfo) => {

const pkgDataPath = path.join(pkgPath, 'package.json')
if (!fs.existsSync(pkgDataPath)) {
console.log(`Skipping ${pkgDataPath}`)
require('console').log(`Skipping ${pkgDataPath}`)
continue
}
const pkgData = require(pkgDataPath)
Expand Down Expand Up @@ -137,7 +137,7 @@ module.exports = (actionInfo) => {
pkgData.files = []
}
pkgData.files.push('native')
console.log(
require('console').log(
'using swc binaries: ',
await exec(
`ls ${path.join(path.dirname(pkgDataPath), 'native')}`
Expand Down
29 changes: 12 additions & 17 deletions test/e2e/test-utils-tests/basic/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'

describe('createNext', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: __dirname,
createNextDescribe(
'createNextDescribe',
{
files: __dirname,
},
({ next }) => {
it('should work', async () => {
const res = await next.fetch('/')
expect(await res.text()).toContain('Hello World')
})
})
afterAll(() => next.destroy())

it('should work', async () => {
const res = await fetchViaHTTP(next.url, '/')
expect(await res.text()).toContain('Hello World')
})
})
}
)
80 changes: 72 additions & 8 deletions test/lib/create-next-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ const { randomBytes } = require('crypto')
const { linkPackages } =
require('../../.github/actions/next-stats-action/src/prepare/repo-setup')()

/**
* These are simple dependencies provided by default. We want to optimize this special case.
*/
const areGenericDependencies = (dependencies) =>
Object.keys(dependencies).length === 6 &&
Object.entries(dependencies).every(([dep, version]) => {
if (dep === 'next') return true
return (
[
'react',
'react-dom',
'typescript',
'@types/react',
'@types/node',
].includes(dep) && version === 'latest'
)
})

async function createNextInstall({
parentSpan,
dependencies,
Expand All @@ -29,6 +47,10 @@ async function createNextInstall({
`next-repo-${randomBytes(32).toString('hex')}${dirSuffix}`
)

require('console').log('Using following temporary directories:')
require('console').log(installDir)
require('console').log(tmpRepoDir)

await rootSpan.traceChild(' enruse swc binary').traceAsyncFn(async () => {
// ensure swc binary is present in the native folder if
// not already built
Expand Down Expand Up @@ -137,15 +159,57 @@ async function createNextInstall({
await rootSpan
.traceChild('run generic install command')
.traceAsyncFn(async () => {
await execa(
'pnpm',
['install', '--strict-peer-dependencies=false'],
{
cwd: installDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: process.env,
const runInstall = async () =>
await execa(
'pnpm',
['install', '--strict-peer-dependencies=false'],
{
cwd: installDir,
stdio: ['ignore', 'inherit', 'inherit'],
env: process.env,
}
)

if (!areGenericDependencies(combinedDependencies)) {
await runInstall()
} else {
const cacheDir = path.join(
origRepoDir,
'test',
'tmp',
'genericInstallCache'
)

const cachedFiles = [
// We can't cache node-modules because .pnpm store must be on the same mount - we can't move it between mountpoints
// 'node_modules',
'pnpm-lock.yaml',
]

if (
await fs
.access(cacheDir)
.then(() => true)
.catch(() => false)
) {
require('console').log(
'We are able to prepopulate pnpm install from cache'
)
cachedFiles.forEach((file) => {
fs.copy(
path.join(cacheDir, file),
path.join(installDir, file)
)
})
}
)

await runInstall()

await fs.ensureDir(cacheDir)
cachedFiles.forEach((file) => {
fs.copy(path.join(installDir, file), path.join(cacheDir, file))
})
}
})
}

Expand Down

0 comments on commit a7b046d

Please sign in to comment.