Skip to content

Commit

Permalink
Merge pull request #87 from smashtestio/feat/start-server
Browse files Browse the repository at this point in the history
Add --start-server arg
  • Loading branch information
sarimarton authored Aug 30, 2023
2 parents 9400893 + 19d537b commit df335e3
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ smashtest
dist
.DS_Store
package-lock.json
start-server-and-test/bundle.js
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
},
"files": [
"src/**",
"dist/**"
"dist/**",
"start-server-and-test/bundle.js"
],
"scripts": {
"build": "tsc && npm run copy-assets",
"build": "tsc && npm run copy-assets && npm run build:ssat",
"build:ssat": "cd start-server-and-test && rm -rf node_modules && rm package-lock.json && npm i && npm run build",
"copy-assets": "rsync -am --include='*.smash' --include='*.html' --include='*/' --exclude='*' src/ dist",
"open-report": "open file://$(pwd)/smashtest/report.html",
"prepublishOnly": "npm run build && eslint . && npm run test:unit",
Expand Down Expand Up @@ -72,6 +74,7 @@
"read-files-promise": "^1.1.1",
"request": "^2.88.0",
"selenium-webdriver": "^4.11.1",
"shell-quote": "^1.8.1",
"sinon": "^14.0.0",
"tiny-invariant": "^1.2.0",
"ts-node": "^10.9.1",
Expand All @@ -90,6 +93,7 @@
"@types/node": "^18.7.18",
"@types/request": "^2.48.8",
"@types/selenium-webdriver": "^4.1.5",
"@types/shell-quote": "^1.7.1",
"@types/sinon": "^10.0.13",
"@types/tough-cookie": "^4.0.2",
"@types/ws": "^8.5.4",
Expand Down
38 changes: 38 additions & 0 deletions src/core/cli.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import fs from 'node:fs';
import path from 'node:path';
import readline from 'node:readline';
import repl from 'node:repl';
import { spawnSync } from 'node:child_process';
// @ts-expect-error - no types
import readFiles from 'read-files-promise';
import { fileURLToPath } from 'url';
import * as Constants from './constants.js';
import { reporter, runner, tree } from './instances.js';
import StepNode from './stepnode.js';
import * as utils from './utils.js';
import shellQuote from 'shell-quote';

// Reading package.json is way simpler already, but it's a parse error for
// eslint, so even // eslint-disable doesn't work. Eslint only supports stage 4
Expand Down Expand Up @@ -193,6 +196,7 @@ Options
--screenshots=<true/false> Whether to take screenshots at each step
--skip-passed=<true/false/file> Whether to skip branches that passed last time (-s/-a)
--step-data=<all/fail/none> Keep step data for all steps, only failed steps, or no steps
--start-server="'<cmd>' <condition>" Starts a server and shuts it down at the end (see npmjs.com/package/start-server-and-test)
--test-server=<url> Location of test server (e.g., http://localhost:4444/wd/hub for selenium server)
--version Output the version of Smashtest (-v)
`);
Expand Down Expand Up @@ -299,6 +303,40 @@ Options
}
break;

case 'start-server': {
const ssatArgs = shellQuote.parse(value);

const err = (msg: string) => {
utils.error(
`${msg}\nExample: --start-server="'npm start' :5001"\n` +
'For more information, see the first and second arguments at https://npmjs.com/package/start-server-and-test'
);
};

if (ssatArgs.some((arg) => typeof arg !== 'string')) {
const parsedAs = JSON.stringify(ssatArgs);
err(`Invalid start-server. It must have 1 or 2 string arguments. (Parsed it as ${parsedAs})`);
}
else if (ssatArgs.length === 0) {
utils.error('Invalid start-server. It must have 1 or 2 arguments.');
}
else if (ssatArgs.length > 2) {
utils.error(
`Invalid start-server. It must have 1 or 2 arguments. (${ssatArgs.length} were provided).`
);
}

const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const ssatBundlePath = path.join(scriptDir, '../../start-server-and-test/bundle.js');
const testCmdLine = process.argv.filter((arg) => !arg.startsWith('--start-server=')).join(' ');

const nodeArgs = [ssatBundlePath, ...ssatArgs, testCmdLine] as string[];
const result = spawnSync(process.argv[0], nodeArgs, { stdio: 'inherit' });

process.exit(result.status ?? void 0);
break;
}

case 'step-data':
if (value === 'all' || value === 'fail' || value === 'none') {
tree.stepDataMode = value;
Expand Down
1 change: 1 addition & 0 deletions start-server-and-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Patched and bundled version of `[email protected]`.
27 changes: 27 additions & 0 deletions start-server-and-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "start-server-and-test_patched",
"version": "1.0.0",
"description": "",
"keywords": [],
"license": "ISC",
"author": "",
"main": "index.js",
"scripts": {
"postinstall": "patch-package",
"build": "rollup -c"
},
"dependencies": {
"patch-package": "^8.0.0",
"start-server-and-test": "2.0.0"
},
"devDependencies": {
"@babel/core": "^7.22.11",
"@babel/preset-env": "^7.22.10",
"@rollup/plugin-babel": "^6.0.3",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-strip": "^3.0.2",
"rollup": "^3.28.1"
}
}
34 changes: 34 additions & 0 deletions start-server-and-test/patches/start-server-and-test+2.0.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Npm library 'patch-package' uses this file.
# This patch fixes a compatibility problem between start-server-and-test and
# smashtest. Track the issue here:
# https://github.com/bahmutov/start-server-and-test/issues/368
# Once it's fixed, this patch can be deleted. (But keep the next one.)
diff --git a/node_modules/start-server-and-test/src/index.js b/node_modules/start-server-and-test/src/index.js
index cca10d0..c84db6e 100644
--- a/node_modules/start-server-and-test/src/index.js
+++ b/node_modules/start-server-and-test/src/index.js
@@ -44,7 +44,7 @@ function waitAndRun ({ start, url, runFn, namedArguments }) {

debug('starting server with command "%s", verbose mode?', start, isDebug())

- const server = execa(start, { shell: true, stdio: 'inherit' })
+ const server = execa(start, { shell: true, stdio: ['ignore', 'inherit', 'inherit'] })
let serverStopped

function stopServer () {
# This is not a bug in the library per se, but it's a dynamic require() call which
# Rollup can't compile into an import statement. And we're ESM.
diff --git a/node_modules/start-server-and-test/src/utils.js b/node_modules/start-server-and-test/src/utils.js
index abd1a69..c5dad2c 100644
--- a/node_modules/start-server-and-test/src/utils.js
+++ b/node_modules/start-server-and-test/src/utils.js
@@ -153,7 +153,8 @@ const isPackageScriptName = command => {
if (!existsSync(packageFilename)) {
return false
}
- const packageJson = require(packageFilename)
+ const fs = require('fs')
+ const packageJson = JSON.parse(fs.readFileSync(packageFilename, 'utf8'))
if (!packageJson.scripts) {
return false
}
41 changes: 41 additions & 0 deletions start-server-and-test/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from '@rollup/plugin-babel';
import json from '@rollup/plugin-json';
import strip from '@rollup/plugin-strip';

const removeShebang = {
name: 'remove-shebang',
transform(code, id) {
if (/node_modules/.test(id)) {
const newCode = code.replace(/^#!.*/, '');
return {
code: newCode,
map: null
};
}
}
};

export default {
input: './node_modules/start-server-and-test/src/bin/start.js',
output: {
file: './bundle.js',
format: 'es'
},
plugins: [
removeShebang,
json(),
resolve({ preferBuiltins: true }),
commonjs(),
babel({
exclude: 'node_modules/**',
babelHelpers: 'bundled',
presets: ['@babel/preset-env']
}),
strip({
include: ['./node_modules/start-server-and-test/**/*.(js|mjs)']
})
],
external: []
};

0 comments on commit df335e3

Please sign in to comment.