diff --git a/packages/electrode-react-webapp/test/spec/hapi17.index.spec.js b/packages/electrode-react-webapp/test/spec/hapi17.index.spec.js index c2716a3de..13487a2ee 100644 --- a/packages/electrode-react-webapp/test/spec/hapi17.index.spec.js +++ b/packages/electrode-react-webapp/test/spec/hapi17.index.spec.js @@ -1144,7 +1144,7 @@ describe("hapi 17 electrode-react-webapp", () => { }); it("should add a nonce value as provided by a function in the config", () => { - configOptions.cspNonceValue = function(request, type) { + configOptions.cspNonceValue = function (request, type) { return `==${type}`; }; @@ -1239,7 +1239,7 @@ describe("hapi 17 electrode-react-webapp", () => { it("should inject critical css with a nonce value provided by a function", () => { configOptions.criticalCSS = "test/data/critical.css"; - configOptions.cspNonceValue = function(request, type) { + configOptions.cspNonceValue = function (request, type) { return `==${type}`; }; @@ -1664,6 +1664,7 @@ describe("hapi 17 electrode-react-webapp", () => { .inject({ method: "GET", url: "/" + }) .then(() => { stdoutIntercept.restore(); @@ -1684,7 +1685,7 @@ describe("hapi 17 electrode-react-webapp", () => { .then(res => { expect(res.result).includes( `` + - `Nested Title` + `Nested Title` ); expect(res.result) .includes(`window._config.ui = {"webappPrefix":""};\n @@ -1698,7 +1699,7 @@ describe("hapi 17 electrode-react-webapp", () => { }); }); - describe("with webpackDev", function() { + describe("with webpackDev", function () { it("should skip if webpack dev is not valid", () => { return electrodeServer(config).then(server => { server.ext({ diff --git a/packages/xarc-create-app/package.json b/packages/xarc-create-app/package.json index cf8ae3b4d..f2a9c4aed 100644 --- a/packages/xarc-create-app/package.json +++ b/packages/xarc-create-app/package.json @@ -4,7 +4,9 @@ "description": "Create react.js/node.js webapp using the Electrode Platform", "main": "dist/index.js", "scripts": { - "prepublishOnly": "webpack" + "prepublishOnly": "webpack", + "test": "mocha", + "coverage": "nyc npm test" }, "publishConfig": { "registry": "https://registry.npmjs.org/", @@ -21,9 +23,9 @@ ], "files": [ "bin", + "dist", "lib", - "template", - "dist" + "template" ], "author": "Electrode (http://www.electrode.io/)", "contributors": [ @@ -33,21 +35,66 @@ "devDependencies": { "@babel/core": "^7.6.2", "@babel/preset-env": "^7.6.2", + "@types/chai": "^4.2.11", + "@types/mocha": "^7.0.2", + "@xarc/module-dev": "^2.2.4", "babel-loader": "^8.0.6", + "chai": "^4.2.0", "chalker": "^1.2.0", "lodash": "^4.17.15", + "mocha": "^7.1.0", + "nyc": "^15.0.0", "opfs": "^1.1.1", "prompts": "^2.3.2", "shcmd": "^0.7.9", + "sinon": "^7.2.6", + "sinon-chai": "^3.3.0", "webpack": "^4.41.0", "webpack-bundle-analyzer": "^3.5.2", "webpack-cli": "^3.3.9", - "xclap": "^0.2.51" + "xclap": "^0.2.51", + "run-verify": "^1.2.5" }, "prettier": { "printWidth": 100, "arrowParens": "avoid", "trailingComma": "none" }, - "dependencies": {} + "mocha": { + "require": [ + "./test/setup.js" + ], + "recursive": true + }, + "nyc": { + "extends": [], + "all": true, + "reporter": [ + "lcov", + "text", + "text-summary" + ], + "include": [ + "src" + ], + "exclude": [ + "*clap.js", + "*clap.ts", + "coverage", + "dist", + "docs", + "gulpfile.js", + "test", + "xrun*.js", + "xrun*.ts", + "template", + "bin" + ], + "check-coverage": true, + "statements": 100, + "branches": 100, + "functions": 100, + "lines": 100, + "cache": true + } } diff --git a/packages/xarc-create-app/test/data/.placeholder b/packages/xarc-create-app/test/data/.placeholder new file mode 100644 index 000000000..e69de29bb diff --git a/packages/xarc-create-app/test/setup.js b/packages/xarc-create-app/test/setup.js new file mode 100644 index 000000000..71c9d82b7 --- /dev/null +++ b/packages/xarc-create-app/test/setup.js @@ -0,0 +1,3 @@ +const chai = require("chai") +const sinon = require("sinon"); + diff --git a/packages/xarc-create-app/test/spec/check-dir.js b/packages/xarc-create-app/test/spec/check-dir.js new file mode 100644 index 000000000..e69de29bb diff --git a/packages/xarc-create-app/test/spec/check-dir.spec.js b/packages/xarc-create-app/test/spec/check-dir.spec.js new file mode 100644 index 000000000..e9fc56d7f --- /dev/null +++ b/packages/xarc-create-app/test/spec/check-dir.spec.js @@ -0,0 +1,34 @@ +const { expect } = require('chai'); +const { runFinally, asyncVerify } = require("run-verify"); +const { Transform } = require('stream'); +const checkDir = require('../../src/check-dir'); +const shcmd = require("shcmd"); +const { execSync, spawn } = require('child_process'); + +describe('checkDir', function () { + const cwd = process.cwd(); + afterEach(() => { + process.chdir(cwd); + }); + + it('checks if directory has existing files', function () { + shcmd.mkdir("test/data/empty-dir"); + process.chdir("test/data/empty-dir"); + asyncVerify( + () => checkDir(process.cwd()), + (ret) => { + expect(ret).to.equal(true) + } + ); + runFinally(() => { + shcmd.rmdir("test/data/empty-dir") + }) + }); + + it('check non empty prompt', async function () { + const output = execSync(`echo 'n' | node -e "require('./src/check-dir')('test/data')"`).toString(); + expect(output).to.include("not empty"); + + }); + +}) \ No newline at end of file diff --git a/packages/xarc-create-app/test/spec/create-app.spec.js b/packages/xarc-create-app/test/spec/create-app.spec.js new file mode 100644 index 000000000..e5f27963a --- /dev/null +++ b/packages/xarc-create-app/test/spec/create-app.spec.js @@ -0,0 +1,25 @@ +const expect = require("chai").expect; +const shcmd = require("shcmd"); +const { resolve } = require("path"); +const { execSync } = require("child_process"); +describe('create app', () => { + beforeEach(() => { + process.chdir(resolve(__dirname, "../../")); + + shcmd.mkdir("test/data/create-app-unit-test"); + process.chdir("test/data/create-app-unit-test"); + + }); + + it('precondition: directory is created, and create app in cwd', () => { + shcmd.exec("echo 'y' | node ../../../src/create-app"); + + }) + afterEach(() => { + process.chdir(resolve(__dirname, "../../")); + + execSync("rm -rf test/data/create-app-unit-test"); + + }); + +}) \ No newline at end of file diff --git a/packages/xarc-create-app/test/spec/index.spec.js b/packages/xarc-create-app/test/spec/index.spec.js new file mode 100644 index 000000000..454c7d360 --- /dev/null +++ b/packages/xarc-create-app/test/spec/index.spec.js @@ -0,0 +1,26 @@ +const expect = require("chai").expect; +const shcmd = require("shcmd"); +const { resolve } = require("path"); +const { execSync } = require("child_process"); + +describe('index', function () { + it('creates directory and then app in it', function () { + process.chdir(resolve(__dirname, "../data")); + shcmd.exec("echo 'y' | node ../../src/index new-app"); + const output = shcmd.exec("ls new-app").stdout; + + expect(output).to.include("src"); + expect(output).to.include("package.json"); + expect(output).to.include("static"); + expect(output).to.include("xclap"); + execSync("rm -rf new-app"); + process.chdir(".."); + }).timeout(10000); + + it("aborts for non-empty directory", function () { + process.chdir(resolve(__dirname, "../..")); + shcmd.exec("echo 'n' | node ./src/index node_modules"); + + }) +}) +//require("../../src/"); diff --git a/packages/xarc-create-app/test/spec/prep-app-dir.spec.js b/packages/xarc-create-app/test/spec/prep-app-dir.spec.js new file mode 100644 index 000000000..f02e8ea55 --- /dev/null +++ b/packages/xarc-create-app/test/spec/prep-app-dir.spec.js @@ -0,0 +1,76 @@ + +const { expect, should } = require("chai"); +const { spawn, execSync } = require("child_process"); +const Fs = require('fs') +require("chai") +describe('prepAppDir', function () { + this.beforeEach(() => { + process.chdir(require("path").resolve(__dirname, '../..')) + }) + const callSubroutineWithArgv2 = arg => { + return spawn("node", ["-e", `process.argv[2]='${arg}'; require('./src/prep-app-dir')()`]); + } + it('requires process.argv2', async function () { + const routine = callSubroutineWithArgv2(""); + var output = "" + routine.stdout.on("data", function (d) { + output += d.toString(); + }) + + routine.on("exit", () => { + expect(output).to.include("pass '.' to use current directory") + }); + }); + + + it('creates directory with name from process.argv2', async function () { + expect(execSync("ls test/data").toString()).to.not.include("hello_app"); + callSubroutineWithArgv2("./test/data/hello_app").on("exit", () => { + expect(execSync("ls test/data").toString()).to.include("hello_app"); + Fs.rmdirSync("test/data/hello_app"); + }); + }); + + it('when it throws a non-EEXIST exception, console logs msg Failed to create..', async function () { + try { + execSync("rm -rf test/data/no-writing && mkdir test/data/no-writing && chmod -w test/data/no-writing"); + const routine = callSubroutineWithArgv2("./test/data/no-writing/new-app"); + } catch (e) { + expect(e.message).to.include("failed to") + execSync("rm -rf test/data/no-writing"); + } + + + }); + it('when process.argv2 ', function () { + // expect(execSync("ls test/data").toString()).to.not.include("hello_app"); + const nodeproc = callSubroutineWithArgv2("."); + var output = "" + nodeproc.stdout.on("data", function (d) { + output += d.toString(); + }) + nodeproc.on("exit", () => { + expect(output).to.include("Using current directory") + }); + }); + it('when process.argv2 is existing directory', async function () { + // expect(execSync("ls test/data").toString()).to.not.include("hello_app"); + await new Promise(resolve => { + const nodeproc = callSubroutineWithArgv2("./test/spec"); + var output = "" + nodeproc.stdout.on("data", function (d) { + output += d.toString(); + }) + nodeproc.on("exit", (cod3) => { + expect(cod3).to.equal(0); + resolve(); + }); + }) + }); + + + + + +}); + diff --git a/packages/xarc-create-app/test/spec/sort-deps.spec.js b/packages/xarc-create-app/test/spec/sort-deps.spec.js new file mode 100644 index 000000000..a1deabd40 --- /dev/null +++ b/packages/xarc-create-app/test/spec/sort-deps.spec.js @@ -0,0 +1,46 @@ +const sortDeps = require("../../src/sort-deps"); +const { resolve } = require("path"); +const _ = require("lodash"); +const { execSync } = require("child_process"); +const { expect } = require("chai"); +const { unlink } = require("fs"); +const { doesNotReject } = require("assert"); + +const xrequire = eval("require"); + +describe('sort-deps', function () { + this.beforeEach(() => { + process.chdir(resolve(__dirname, "../..")); + }) + it('sorts packages by keys', async function () { + + await new Promise(resolve => { + const pkg = xrequire(resolve(__dirname, "../../template/_package"))({}, _.merge); + sortDeps(pkg); + const sortedDeps = pkg.dependencies; + const sortedStr = Object.keys(sortedDeps).join("\n"); + const qaSortedStr = execSync(`echo '${sortedStr}' > temp.txt && cat temp.txt |sort`).toString(); + expect(sortedStr.trim()).to.equal(qaSortedStr.trim()); + unlink('temp.txt', () => { + resolve(); + }); + }) + }); + + it("skip undefined sections", async () => { + await new Promise(resolve => { + + const pkg = { + dependencies: { "a": 1, "b": 1 } + }; + sortDeps(pkg); + const sortedDeps = pkg.dependencies; + const sortedStr = Object.keys(sortedDeps).join("\n"); + const qaSortedStr = execSync(`echo '${sortedStr}' > temp.txt && cat temp.txt |sort`).toString(); + expect(sortedStr.trim()).to.equal(qaSortedStr.trim()); + unlink('temp.txt', () => { + resolve(); + }); + }); + }); +}) \ No newline at end of file diff --git a/packages/xarc-create-app/xrun-tasks.js b/packages/xarc-create-app/xrun-tasks.js new file mode 100644 index 000000000..bb0309ccd --- /dev/null +++ b/packages/xarc-create-app/xrun-tasks.js @@ -0,0 +1,2 @@ +import { loadTasks } from "@xarc/module-dev"; +loadTasks();