Skip to content

Commit

Permalink
test: addition of smoke tests
Browse files Browse the repository at this point in the history
* chore: add smoke test for generator-options command - stdout echoed (#163)

* chore: generator-options smoke test added (#163)

* chore: wip smoke test-generators (#163)

* chore: write smoke test in JS, all three commands pass

* chore: pass smoke test work to JS instead of Github Actions

* chore: move noisy logs to log file

* chore: log available on step failure, clean up comments

* chore: revert delete of workflow files

* fix: incorrectly removed line

* chore: print installed JS and TS generator versions in smoke test

* chore: pass test-generators if it has >= passes cpw master
  • Loading branch information
ms14981 authored Apr 14, 2023
1 parent ee2cfcc commit 651f7f4
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Smoke Tests

on:
push:
branches:
- "**"
pull_request:
branches:
- "**"

jobs:
smoke-tests:
name: Smoke Tests
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'npm'

- name: Install Node.js dependencies
run: |
npm install
- id: smoke_test
name: Smoke Test generator-options, test-generators and forge commands
run: |
npm run test:smoke
- id: show_full_log
name: Show Full Log
if: success() || failure()
run: |
cat log.txt
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"prepare": "husky install",
"test:generators": "openapi-forge test-generators",
"test": "jest",
"test:smoke": "node smoke-test.js",
"format:check:all": "prettier --check .",
"format:write:all": "prettier --write .",
"lint:check:all": "eslint .",
Expand Down
119 changes: 119 additions & 0 deletions smoke-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
const shell = require("shelljs");
const fs = require("fs");

const jsGeneratorLocation = "smoke-js";
const tsGeneratorLocation = "smoke-ts";

const cloneRepoToPath = (repoPath, destPath, returnPath) => {
const cloneStdout = shell.exec(`git clone ${repoPath} ${destPath}`, {
silent: true,
}).stdout;
shell.cd(destPath);
const installStdout = shell.exec("npm install", { silent: true }).stdout;
const latestVersion = getLatestGitVersionTag(repoPath);
shell.cd(returnPath);
return cloneStdout + "\r\n" + latestVersion + "\r\n" + installStdout;
};

const getLatestGitVersionTag = (repoPath) =>
"Version of " +
repoPath +
" installed: " +
shell.exec(`git tag --sort=committerdate | tail -1`, {
silent: true,
}).stdout;

const gitCheckout = (branch) =>
shell.exec(`git fetch && git checkout ${branch}`, { silent: true }).stdout;

const runTestGenerators = () =>
JSON.parse(
shell.exec(
`node ./src/index.js test-generators --generators ${tsGeneratorLocation} --format json --logLevel quiet`,
{ silent: true }
).stdout
);

// NOTE: The JS and TS generators are installed in different relative paths because generator-options and test-generators require different relative paths.
const jsInstallStdout = cloneRepoToPath(
"https://github.com/ScottLogic/openapi-forge-javascript",
jsGeneratorLocation,
".."
);
const tsInstallStdout = cloneRepoToPath(
"https://github.com/ScottLogic/openapi-forge-typescript",
`../${tsGeneratorLocation}`,
"../openapi-forge"
);

// Smoke test generator-options command
const generatorOptionsStdout = shell.exec(
`node ./src/index.js generator-options ${jsGeneratorLocation}`,
{ silent: true }
).stdout;

if (generatorOptionsStdout.includes("moduleFormat")) {
console.log("generator-options command succeeded");
} else {
console.error(
"generator-options command failed. Expected moduleFormat to be an option in the JavaScript generator."
);
process.exitCode = 1;
}

// Smoke test test-generators command by comparing the current number of passing tests in the TS generator to the number passing on this PR
const currentCommitName = shell.exec("git rev-parse HEAD", { silent: true });
const checkoutLogs1 = gitCheckout("master");
const previousRun = runTestGenerators();

const { passed: expectedPassed } = previousRun[tsGeneratorLocation];

const checkoutLogs2 = gitCheckout(currentCommitName);
const testGeneratorsStdout = runTestGenerators();

const { scenarios, passed } = testGeneratorsStdout[tsGeneratorLocation];

if (scenarios > 0 && passed >= expectedPassed) {
console.log("test-generators command succeeded");
} else {
console.error(
"test-generators command failed. Expected " +
expectedPassed +
" passes but there were only " +
passed +
" passes."
);
process.exitCode = 1;
}

// Smoke test forge command
const tempFolder = "temp-csharp";
const forgeStdout = shell.exec(
`node ./src/index.js forge https://petstore3.swagger.io/api/v3/openapi.json https://github.com/ScottLogic/openapi-forge-csharp.git -o ${tempFolder}`,
{ silent: true }
).stdout;
if (forgeStdout.includes("SUCCESSFUL")) {
console.log("forge command succeeded");
} else {
console.error("forge command failed.");
process.exitCode = 1;
}

// Clean up
shell.rm("-rf", jsGeneratorLocation, `../${tsGeneratorLocation}`, tempFolder);

fs.writeFileSync(
`log.txt`,
[
jsInstallStdout,
tsInstallStdout,
generatorOptionsStdout,
"run on master:",
checkoutLogs1,
JSON.stringify(previousRun),
"run on current branch:",
checkoutLogs2,
JSON.stringify(testGeneratorsStdout),
forgeStdout,
].join("\r\n")
);

0 comments on commit 651f7f4

Please sign in to comment.