Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
fix(server): paths on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 22, 2022
1 parent fde1beb commit 1642722
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 114 deletions.
3 changes: 2 additions & 1 deletion packages/peeky-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
},
"dependencies": {
"@peeky/utils": "^0.13.0",
"chokidar": "^3.5.3",
"consola": "^2.15.0",
"fs-extra": "^10.0.0",
"pathe": "^0.2.0",
"reactive-fs": "^0.4.1",
"shortid": "^2.2.16",
"vite": "^2.7.10"
},
Expand Down
2 changes: 0 additions & 2 deletions packages/peeky-config/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { C8Options, PeekyConfig } from './types'

export const peekyConfigFileMatch = ['**/peeky.config.(js|ts)']

const defaultCoverageExcludes = [
'coverage/**',
'packages/*/test{,s}/**',
Expand Down
46 changes: 0 additions & 46 deletions packages/peeky-config/src/fs.ts

This file was deleted.

45 changes: 36 additions & 9 deletions packages/peeky-config/src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import fs from 'fs'
import { join } from 'pathe'
import fs from 'fs-extra'
import path from 'pathe'
import consola from 'consola'
import shortid from 'shortid'
import { fixWindowsAbsoluteFileUrl } from '@peeky/utils'
import type { PeekyConfig } from './types'
import { setupConfigContentLoader } from './fs.js'
import { transformConfigCode } from './transform.js'
import { defaultPeekyConfig } from './defaults.js'
import { mergeConfig } from './util.js'
Expand All @@ -15,17 +14,45 @@ export interface PeekyConfigLoaderOptions {
glob?: string | string[]
}

export async function setupConfigLoader (options: PeekyConfigLoaderOptions = {}) {
const contentLoader = await setupConfigContentLoader(options.baseDir, options.glob)
const configFileNames = [
'peeky.config.ts',
'peeky.config.js',
'.peeky.ts',
'.peeky.js',
]

export function resolveConfigFile (cwd: string = process.cwd()): string {
let { root } = path.parse(cwd)
let dir = cwd

// Fix for windows, waiting for pathe to fix this: https://github.com/unjs/pathe/issues/5
if (root === '' && dir[1] === ':') {
root = dir.substring(0, 2)
}

while (dir !== root) {
for (const fileName of configFileNames) {
const searchPath = path.join(dir, fileName)
if (fs.existsSync(searchPath)) {
return searchPath
}
}
dir = path.dirname(dir)
}

return null
}

export async function setupConfigLoader (options: PeekyConfigLoaderOptions = {}) {
async function loadConfig (loadFromVite = true): Promise<PeekyConfig> {
const file = contentLoader.getConfigPath()
const resolvedPath = join(options.baseDir || process.cwd(), file + shortid() + '.temp.mjs')
const cwd = options.baseDir || process.cwd()
const file = await resolveConfigFile(cwd)
const resolvedPath = file + shortid() + '.temp.mjs'
try {
let config: PeekyConfig = {}

if (file) {
const rawCode = await contentLoader.loadConfigFileContent()
const rawCode = await fs.readFile(file, 'utf8')
const result = await transformConfigCode(rawCode, file)
fs.writeFileSync(resolvedPath, result.code)
config = (
Expand Down Expand Up @@ -60,7 +87,7 @@ export async function setupConfigLoader (options: PeekyConfigLoaderOptions = {})
}

function destroy () {
return contentLoader.destroy()
// noop
}

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/peeky-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
"@types/sinon": "^9.0.10",
"c8": "^7.11.0",
"chalk": "^5.0.0",
"chokidar": "^3.5.3",
"consola": "^2.15.0",
"diffable-html": "^5.0.0",
"expect": "^27.0.6",
"fast-glob": "^3.2.11",
"fs-extra": "^10.0.0",
"fs-monkey": "^1.0.3",
"happy-dom": "^2.24.4",
Expand All @@ -49,7 +51,6 @@
"pathe": "^0.2.0",
"pragma": "^1.0.0",
"pretty-format": "^27.4.2",
"reactive-fs": "^0.4.1",
"shortid": "^2.2.16",
"sinon": "^9.2.3",
"slugify": "^1.6.0",
Expand Down
12 changes: 4 additions & 8 deletions packages/peeky-runner/src/run-all.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { performance } from 'perf_hooks'
import { createReactiveFileSystem } from 'reactive-fs'
import { ProgramPeekyConfig } from '@peeky/config'
import glob from 'fast-glob'
import { setupRunner } from './runner.js'
import { getStats } from './stats.js'
import { createConsoleFancyReporter } from './reporters/console-fancy.js'
Expand Down Expand Up @@ -28,20 +28,16 @@ export async function runAllTests (config: ProgramPeekyConfig, options: RunAllOp
createConsoleFancyReporter(),
]

const testFiles = await createReactiveFileSystem({
baseDir: config.targetDirectory,
glob: config.match,
ignored: config.ignored,
let fileList = await glob(config.match, {
cwd: config.targetDirectory,
ignore: Array.isArray(config.ignored) ? config.ignored : [config.ignored],
})

const runner = await setupRunner({
config,
testFiles,
reporters,
})

let fileList = runner.testFiles.list()

if (options.quickTestFilter) {
const reg = new RegExp(options.quickTestFilter, 'i')
fileList = fileList.filter(f => reg.test(f))
Expand Down
11 changes: 3 additions & 8 deletions packages/peeky-runner/src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { relative, resolve } from 'pathe'
import { ReactiveFileSystem } from 'reactive-fs'
import fs from 'fs-extra'
import Tinypool, { Options as PoolOptions } from 'tinypool'
import { createServer, mergeConfig } from 'vite'
Expand All @@ -13,7 +12,6 @@ import { clearCoverageTemp } from './coverage.js'

export interface RunnerOptions {
config: ProgramPeekyConfig
testFiles: ReactiveFileSystem
reporters: Reporter[]
}

Expand Down Expand Up @@ -61,7 +59,6 @@ export async function setupRunner (options: RunnerOptions) {
}

const pool = new Tinypool(poolOptions)
const { testFiles } = options

async function runTestFileWorker (options: RunTestFileOptions): Promise<Awaited<ReturnType<typeof rawRunTestFile>> & { deps: string[] }> {
const suiteMap: { [id: string]: ReporterTestSuite } = {}
Expand Down Expand Up @@ -179,10 +176,10 @@ export async function setupRunner (options: RunnerOptions) {
}

async function runTestFile (relativePath: string, clearDeps: string[] = [], updateSnapshots = false) {
const file = testFiles.files[relativePath]
if (file) {
const file = resolve(ctx.options.config.targetDirectory, relativePath)
if (fs.existsSync(file)) {
const result = await runTestFileWorker({
entry: file.absolutePath,
entry: file,
config: serializableConfig,
coverage: {
root: ctx.options.config.targetDirectory,
Expand All @@ -202,14 +199,12 @@ export async function setupRunner (options: RunnerOptions) {
}

async function close () {
await testFiles.destroy()
await pool.destroy()
await viteServer.close()
clearOnMessage()
}

return {
testFiles,
runTestFile,
close,
onMessage,
Expand Down
4 changes: 2 additions & 2 deletions packages/peeky-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
"consola": "^2.15.0",
"express": "^4.17.1",
"express-history-api-fallback": "^2.2.1",
"fs-extra": "^9.0.1",
"fast-glob": "^3.2.11",
"fs-extra": "^10.0.0",
"graphql": "^15.4.0",
"graphql-ws": "^5.5.5",
"launch-editor": "^2.2.1",
"nexus": "^1.0.0",
"object-inspect": "^1.11.0",
"pathe": "^0.2.0",
"random-emoji": "^1.0.2",
"reactive-fs": "^0.4.1",
"shortid": "^2.2.16",
"slugify": "^1.6.0",
"ws": "^8.3.0"
Expand Down
2 changes: 0 additions & 2 deletions packages/peeky-server/src/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { ReactiveFileSystem } from 'reactive-fs'
import { PubSubEngine } from 'apollo-server-express'
import { PeekyConfig } from '@peeky/config/dist'

export interface Context {
config: PeekyConfig
reactiveFs: ReactiveFileSystem
pubsub: PubSubEngine
}
4 changes: 2 additions & 2 deletions packages/peeky-server/src/schema/Run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { relative } from 'pathe'
import fs from 'fs-extra'
import { fileURLToPath } from 'url'
import { performance } from 'perf_hooks'
import { arg, extendType, idArg, inputObjectType, nonNull, objectType } from 'nexus'
Expand Down Expand Up @@ -262,7 +263,6 @@ export async function startRun (ctx: Context, id: string) {
if (!runner) {
runner = await setupRunner({
config: toProgramConfig(ctx.config),
testFiles: ctx.reactiveFs,
reporters: [],
})
} else {
Expand Down Expand Up @@ -309,7 +309,7 @@ export async function startRun (ctx: Context, id: string) {
const [suiteId, testId, duration, error] = message.args
const testFile = testSuites.find(s => s.id === suiteId).runTestFile.testFile
const { line, col } = getErrorPosition(testFile.relativePath, error.stack)
const lineSource = (await ctx.reactiveFs.files[testFile.relativePath].waitForContent).split('\n')[line - 1]
const lineSource = fs.existsSync(testFile.absolutePath) ? (await fs.readFile(testFile.absolutePath, 'utf8')).split('\n')[line - 1] : ''
await updateTest(ctx, suiteId, testId, {
status: 'error',
duration,
Expand Down
34 changes: 29 additions & 5 deletions packages/peeky-server/src/schema/TestFile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { join } from 'pathe'
import { join, normalize } from 'pathe'
import glob from 'fast-glob'
import chokidar from 'chokidar'
import { extendType, idArg, intArg, nonNull, objectType, stringArg } from 'nexus'
import launchEditor from 'launch-editor'
import type { Context } from '../context'
Expand Down Expand Up @@ -120,9 +122,19 @@ export interface TestFileData {
export let testFiles: TestFileData[] = []

export async function loadTestFiles (ctx: Context) {
testFiles = ctx.reactiveFs.list().map(path => createTestFile(ctx, path))
testFiles = (await glob(ctx.config.match, {
cwd: ctx.config.targetDirectory,
ignore: Array.isArray(ctx.config.ignored) ? ctx.config.ignored : [ctx.config.ignored],
})).map(path => createTestFile(ctx, normalize(path)))

const watcher = chokidar.watch(ctx.config.match, {
cwd: ctx.config.targetDirectory,
ignored: ctx.config.ignored,
ignoreInitial: true,
})

ctx.reactiveFs.onFileAdd(async (relativePath) => {
async function onFileChange (relativePath: string) {
relativePath = normalize(relativePath)
let testFile: TestFileData = testFiles.find(f => f.relativePath === relativePath)
if (testFile) {
await updateTestFile(ctx, testFile.id, {
Expand All @@ -135,9 +147,13 @@ export async function loadTestFiles (ctx: Context) {
ctx.pubsub.publish(TestFileAdded, {
testFile: testFile,
} as TestFileAddedPayload)
})
}

ctx.reactiveFs.onFileRemove((relativePath) => {
watcher.on('add', file => onFileChange(file))
watcher.on('change', file => onFileChange(file))

watcher.on('unlink', (relativePath) => {
relativePath = normalize(relativePath)
const testFile = testFiles.find(f => f.relativePath === relativePath)
if (testFile) {
testFile.deleted = true
Expand All @@ -146,6 +162,14 @@ export async function loadTestFiles (ctx: Context) {
} as TestFileRemovedPayload)
}
})

async function destroy () {
await watcher.close()
}

return {
destroy,
}
}

export async function updateTestFile (ctx: Context, id: string, data: Partial<Omit<TestFileData, 'id'>>) {
Expand Down
7 changes: 0 additions & 7 deletions packages/peeky-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { dirname, join } from 'pathe'
import HTTP from 'http'
import { fileURLToPath } from 'url'
import { createRequire } from 'module'
import { createReactiveFileSystem } from 'reactive-fs'
import { ApolloServer, PubSub } from 'apollo-server-express'
import express from 'express'
import historyFallback from 'express-history-api-fallback'
Expand Down Expand Up @@ -39,17 +38,11 @@ export async function createServer () {
const configLoader = await setupConfigLoader()
const config = await configLoader.loadConfig()

const reactiveFs = await createReactiveFileSystem({
baseDir: config.targetDirectory,
glob: config.match,
ignored: config.ignored,
})
const pubsub = new PubSub()

function createContext (): Context {
return {
config,
reactiveFs,
pubsub,
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/peeky-server/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function getSrcFile (path: string) {
}

export function getErrorPosition (filePath: string, stack: string) {
const result = new RegExp(`${filePath}:(\\d+):(\\d+)`).exec(stack)
const result = new RegExp(`${filePath}:(\\d+):(\\d+)`).exec(stack.replace(/\\/g, '/'))
if (result) {
const [_, line, col] = result
return {
Expand Down

0 comments on commit 1642722

Please sign in to comment.