Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup console output #51

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions build.js → build.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const esbuild = require('esbuild')
const yargs = require('yargs')
const { nodeExternalsPlugin } = require('esbuild-node-externals')
/*eslint-env node*/

import { build } from 'esbuild'
import yargs from 'yargs'
import { nodeExternalsPlugin } from 'esbuild-node-externals'

const argv = yargs(process.argv)
.alias('w', 'watch')
Expand All @@ -17,30 +19,26 @@ if (argv.watch) {
}

// // Build the CLI
esbuild
.build({
entryPoints: ['lib/cli.ts'],
outfile: 'build/cli.js',
build({
entryPoints: ['lib/cli/index.ts'],
outfile: 'build/cli.js',
bundle: true,
platform: 'node',
plugins: [nodeExternalsPlugin()],
watch,
external: ['chrome-aws-lambda', 'puppeteer-core'],
}).catch(() => process.exit(1))

function buildSimpleFile(file, outfile, platform = 'browser') {
build({
entryPoints: [file],
outfile: `build/${outfile}.js`,
platform,
bundle: true,
platform: 'node',
plugins: [nodeExternalsPlugin()],
watch,
external: ['chrome-aws-lambda', 'puppeteer-core'],
})
.catch(() => process.exit(1))

function buildSimpleFile(file, outfile, platform = 'browser') {
esbuild
.build({
entryPoints: [file],
outfile: `build/${outfile}.js`,
platform,
bundle: true,
plugins: [nodeExternalsPlugin()],
external: ['chrome-aws-lambda', 'puppeteer-core'],
watch,
})
.catch(() => process.exit(1))
watch,
}).catch(() => process.exit(1))
}

buildSimpleFile('lib/webpack/webpack-client.ts', 'webpack-client', 'node')
Expand Down
54 changes: 54 additions & 0 deletions lib/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node

// @ts-expect-error server is not typed
import Server from '../server'
import initZen from '../index'
import yargs from 'yargs'
import runRemote from './run_remote'

export type CLIOptions = {
logging: boolean
maxAttempts: number
debug: boolean
configFile: string
junit: string
}

yargs(process.argv.slice(2))
.usage('$0 <cmd> [configFile]')
.command(
['local [configFile]', 'server [configFile]'],
'Run zen with a local server',
// @ts-expect-error yargs changed their type def but this pattern still works
(yargs: yargs.Argv) => {
yargs.positional('file', {
type: 'string',
describe: 'Path to the config file',
})
},
async (argv: CLIOptions) => {
await initZen(argv.configFile)
new Server()
}
)
.command(
'remote [configFile]',
'Run zen in the console',
// @ts-expect-error yargs changed their type def but this pattern still works
(yargs: yargs.Argv) => {
yargs.positional('file', {
type: 'string',
describe: 'Path to the config file',
})
},
async (argv: CLIOptions) => {
const zen = await initZen(argv.configFile)
runRemote(zen, argv)
}
)
.options({
logging: { type: 'boolean', default: false },
maxAttempts: { type: 'number', default: 3 },
debug: { type: 'boolean', default: false },
junit: { type: 'string', default: '' },
}).argv
108 changes: 50 additions & 58 deletions lib/cli.ts → lib/cli/run_remote.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,67 +1,44 @@
#!/usr/bin/env node

// @ts-expect-error server is not typed
import Server from './server'
import initZen from './index'
import yargs from 'yargs'
import { invoke, workTests } from './util'
import * as Profiler from './profiler'
import { Zen } from './types'
import { invoke, workTests } from '../util'
import * as Profiler from '../profiler'
import { Zen } from '../types'
import { CLIOptions } from './index'
import { encodeXML } from 'entities'
import { writeFileSync } from 'fs'
import { join } from 'path'

type testFailure = {
resolved?: boolean
fullName: string
attempts: number
error?: string
time: number
}

export type CLIOptions = {
logging: boolean
maxAttempts: number
debug: boolean
configFile: string
}

yargs(process.argv.slice(2))
.usage('$0 <cmd> [configFile]')
.command(
['local [configFile]', 'server [configFile]'],
'Run zen with a local server',
// @ts-expect-error yargs changed their type def but this pattern still works
(yargs: yargs.Argv) => {
yargs.positional('file', {
type: 'string',
describe: 'Path to the config file',
})
},
async (argv: CLIOptions) => {
await initZen(argv.configFile)
new Server()
}
)
.command(
'remote [configFile]',
'Run zen in the console',
// @ts-expect-error yargs changed their type def but this pattern still works
(yargs: yargs.Argv) => {
yargs.positional('file', {
type: 'string',
describe: 'Path to the config file',
})
},
async (argv: CLIOptions) => {
const zen = await initZen(argv.configFile)
run(zen, argv)
}
)
.options({
logging: { type: 'boolean', default: false },
maxAttempts: { type: 'number', default: 3 },
debug: { type: 'boolean', default: false },
}).argv

type TestResultsMap = Record<string, testFailure>

function resultsToXML(results: TestResultsMap): string {
const failures = Object.values(results)

const xml = `<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="jest tests" tests="${failures.length}" failures="${
failures.length
}" time="0">

${failures
.map((failure) => {
return ` <testcase name="${encodeXML(failure.fullName)}" time="${
failure.time
}">
<failure message="${encodeXML(failure.error || '')}"></failure>
</testcase>`
})
.join('\n')}
</testsuite>
</testsuites>`
return xml
}

async function runTests(
zen: Zen,
opts: CLIOptions,
Expand Down Expand Up @@ -138,18 +115,22 @@ function combineFailures(
// Reset the error state for all the previous tests, that way if they
// succeed it will report only as a flake
for (const testName in failures) {
failures[testName].error = undefined
failures[testName].resolved = true
}

for (const testName in currentFailures) {
const prevFailure = failures[testName]
const curFailure = currentFailures[testName]

if (!prevFailure) {
failures[testName] = curFailure
failures[testName] = {
...curFailure,
resolved: false,
}
} else {
failures[testName] = {
...prevFailure,
resolved: false,
error: curFailure.error,
time: prevFailure.time + curFailure.time,
attempts: prevFailure.attempts + curFailure.attempts,
Expand All @@ -160,7 +141,10 @@ function combineFailures(
return failures
}

async function run(zen: Zen, opts: CLIOptions) {
export default async function runRemote(
zen: Zen,
opts: CLIOptions
): Promise<void> {
try {
let t0 = Date.now()
if (zen.webpack) {
Expand Down Expand Up @@ -254,7 +238,15 @@ async function run(zen: Zen, opts: CLIOptions) {
failCount === 1 ? '' : 's'
}`
)
process.exit(failCount ? 1 : 0)
if (opts.junit && failures) {
const xml = resultsToXML(failures)

console.log('Writing results to ' + opts.junit)
writeFileSync(join(process.cwd(), opts.junit), xml)
process.exit(0)
} else {
process.exit(failCount ? 1 : 0)
}
} catch (e) {
console.error(e)
process.exit(1)
Expand Down
2 changes: 2 additions & 0 deletions lib/head.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env browser */

let socket = null,
store = null

Expand Down
1 change: 0 additions & 1 deletion lib/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default class Journal {
entry.tPass = test.time
}

if (test.time > 2000) console.log(`{${test.time}} - ${test.fullName}`)
this.lazyFlush()
}

Expand Down
11 changes: 2 additions & 9 deletions lib/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export const workTests = async (
const deflakeLimit = opts.deflakeLimit || 3
for (let attempt = 1; attempt <= deflakeLimit; attempt++) {
const remainingTests = getRemainingTests()
console.log('REMAINING TESTS', remainingTests)
if (remainingTests.length === 0) break

await runTestSet(remainingTests, tab)
Expand Down Expand Up @@ -104,7 +103,6 @@ export const workTests = async (
time: 0, // TODO figure out a good way to get time
})
} else {
console.log('UNKOWN ERROR')
console.error(e)
}
}
Expand All @@ -129,7 +127,6 @@ export const sync = async (
): Promise<{ needed: File[] }> => {
if (!process.env.ASSET_BUCKET) throw new Error('ASSET_BUCKET not set')
const bucket = process.env.ASSET_BUCKET
console.log('bucket', bucket)
const s3 = new AWS.S3({ params: { Bucket: bucket } })

// Write the updated session manifest to S3
Expand All @@ -145,17 +142,15 @@ export const sync = async (
// to check, and S3 is pruned to have less than 2k files. Blame this comment for an example.
const needed: File[] = []
const toCheck = manifest.files.filter((f) => f.toCheck)
console.log(`Checking ${toCheck.length} files`)
await Promise.all(
toCheck.map(async (f) => {
try {
const resp = await s3
await s3
.headObject({
Bucket: bucket,
Key: f.versionedPath,
})
.promise()
console.log('Found', f.versionedPath, resp)
} catch (e) {
if (e instanceof Error) {
const error = e as AWS.AWSError
Expand All @@ -169,7 +164,6 @@ export const sync = async (
)

await manifestWrite
console.log('Manifest written')
return { needed }
}

Expand All @@ -187,7 +181,6 @@ async function prepareChrome({ sessionId }: { sessionId: string }) {
console.log('Chrome is already setup!')
}

console.log('Opening tab')
return await wrapper.openTab(
process.env.GATEWAY_URL + '/index.html',
sessionId,
Expand Down Expand Up @@ -218,7 +211,7 @@ async function getManifest(sessionId: string) {
manifest.assetUrl = `https://s3-${process.env.AWS_REGION}.amazonaws.com/${bucket}`
return manifest
} catch (e) {
console.log(e)
console.error('Error getting manifest: ', e)
return null
}
}
1 change: 0 additions & 1 deletion lib/latte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ declare global {
try {
for (const test of tests) {
if (!test.fn) continue
console.log(`${test.fullName}`)
Latte.currentTest = test
whenCurrentTestFinished = new RealPromise(
(res) => (currentTestResolve = res)
Expand Down
1 change: 0 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ module.exports = class Server {
const result = results.at(-1)

if (!result) {
console.log(test, response.results, results)
return {
fullName: test,
attempts: 0,
Expand Down
16 changes: 2 additions & 14 deletions lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,8 @@ export async function workTests(

// If there are timeouts, then keep calling workTests again with the unresolved tests
if (timeoutTests.length > 0 && rerun) {
console.log('RERUNNING DUE TO LAMBDA TIMEOUT', timeoutTests)
await timeout(30_000)
// We don't want this going on endlessly, because there are other errors we may want to do work
const newResponse = await workTests(
zen,
{ ...args, testNames: timeoutTests },
false
)
timeoutTests.forEach((test) => {
response.results[test] = [
...response.results[test],
...newResponse.results[test],
]
})
console.log('Delaying tests a bit due to lambda timeout')
await timeout(10_000)
}

return response
Expand Down
2 changes: 0 additions & 2 deletions lib/webpack/webpack-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module.hot?.accept((err) => {
// TODO I think this might be the source of refresh looping
console.log('LOOPING')
if (err) location.reload()
})

Expand Down
Loading