Unverified Commit a7b046d9 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Cache package lock when running tests to speed up installation (#44520)

parent b6530bb3
...@@ -84,7 +84,7 @@ module.exports = (actionInfo) => { ...@@ -84,7 +84,7 @@ module.exports = (actionInfo) => {
pkgs = await fs.readdir(path.join(repoDir, 'packages')) pkgs = await fs.readdir(path.join(repoDir, 'packages'))
} catch (err) { } catch (err) {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
console.log('no packages to link') require('console').log('no packages to link')
return pkgPaths return pkgPaths
} }
throw err throw err
...@@ -105,7 +105,7 @@ module.exports = (actionInfo) => { ...@@ -105,7 +105,7 @@ module.exports = (actionInfo) => {
const pkgDataPath = path.join(pkgPath, 'package.json') const pkgDataPath = path.join(pkgPath, 'package.json')
if (!fs.existsSync(pkgDataPath)) { if (!fs.existsSync(pkgDataPath)) {
console.log(`Skipping ${pkgDataPath}`) require('console').log(`Skipping ${pkgDataPath}`)
continue continue
} }
const pkgData = require(pkgDataPath) const pkgData = require(pkgDataPath)
...@@ -137,7 +137,7 @@ module.exports = (actionInfo) => { ...@@ -137,7 +137,7 @@ module.exports = (actionInfo) => {
pkgData.files = [] pkgData.files = []
} }
pkgData.files.push('native') pkgData.files.push('native')
console.log( require('console').log(
'using swc binaries: ', 'using swc binaries: ',
await exec( await exec(
`ls ${path.join(path.dirname(pkgDataPath), 'native')}` `ls ${path.join(path.dirname(pkgDataPath), 'native')}`
......
import { createNext } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
describe('createNext', () => { createNextDescribe(
let next: NextInstance 'createNextDescribe',
{
beforeAll(async () => { files: __dirname,
next = await createNext({ },
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')
})
})
...@@ -7,6 +7,24 @@ const { randomBytes } = require('crypto') ...@@ -7,6 +7,24 @@ const { randomBytes } = require('crypto')
const { linkPackages } = const { linkPackages } =
require('../../.github/actions/next-stats-action/src/prepare/repo-setup')() 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({ async function createNextInstall({
parentSpan, parentSpan,
dependencies, dependencies,
...@@ -29,6 +47,10 @@ async function createNextInstall({ ...@@ -29,6 +47,10 @@ async function createNextInstall({
`next-repo-${randomBytes(32).toString('hex')}${dirSuffix}` `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 () => { await rootSpan.traceChild(' enruse swc binary').traceAsyncFn(async () => {
// ensure swc binary is present in the native folder if // ensure swc binary is present in the native folder if
// not already built // not already built
...@@ -137,15 +159,57 @@ async function createNextInstall({ ...@@ -137,15 +159,57 @@ async function createNextInstall({
await rootSpan await rootSpan
.traceChild('run generic install command') .traceChild('run generic install command')
.traceAsyncFn(async () => { .traceAsyncFn(async () => {
await execa( const runInstall = async () =>
'pnpm', await execa(
['install', '--strict-peer-dependencies=false'], 'pnpm',
{ ['install', '--strict-peer-dependencies=false'],
cwd: installDir, {
stdio: ['ignore', 'inherit', 'inherit'], cwd: installDir,
env: process.env, 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))
})
}
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment