Commit 8122d821 authored by Tim Neutkens's avatar Tim Neutkens Committed by Joe Haddad

Handle empty directory (#7876)

* Add test for empty directory

* Fix test re-runs

* Handle empty `pages/` directory
parent 70b8bed4
......@@ -9,6 +9,7 @@ import { ampValidation } from '../build/output/index'
import * as Log from '../build/output/log'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import Watchpack from 'watchpack'
import fs from 'fs'
import {
getRouteMatcher,
getRouteRegex,
......@@ -99,9 +100,22 @@ export default class DevServer extends Server {
return
}
let resolved = false
return new Promise(resolve => {
const pagesDir = join(this.dir, 'pages')
// Watchpack doesn't emit an event for an empty directory
fs.readdir(pagesDir, (_, files) => {
if (files && files.length) {
return
}
if (!resolved) {
resolve()
resolved = true
}
})
let wp = (this.webpackWatcher = new Watchpack())
wp.watch([], [pagesDir], 0)
......@@ -132,7 +146,10 @@ export default class DevServer extends Server {
match: getRouteMatcher(getRouteRegex(page))
}))
resolve()
if (!resolved) {
resolve()
resolved = true
}
})
})
}
......
module.exports = {
onDemandEntries: {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60
}
}
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import fs from 'fs'
import { fetchViaHTTP, findPort, launchApp, killApp } from 'next-test-utils'
const context = {}
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5
describe('Empty Project', () => {
beforeAll(async () => {
fs.unlinkSync(join(__dirname, '..', 'pages', '.gitkeep'))
context.appPort = await findPort()
context.server = await launchApp(join(__dirname, '../'), context.appPort)
})
const fetch = (p, q) => fetchViaHTTP(context.appPort, p, q, { timeout: 5000 })
it('Should not time out and return 404', async () => {
const res = await fetch('/')
expect(res.status).toBe(404)
})
afterAll(() => {
killApp(context.server)
fs.closeSync(fs.openSync(join(__dirname, '..', 'pages', '.gitkeep'), 'w'))
})
})
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