diff --git a/README.md b/README.md index 0f0e1c06..ea80db3e 100644 --- a/README.md +++ b/README.md @@ -132,13 +132,15 @@ nwbuild({ > Deprecation warning: From v4.6.0 onward, run mode is deprecated. This logic has been ported over to `nwjs/npm-installer` repo and will be removed in the next major release. ```javascript -nwbuild({ +const nwProcess = await nwbuild({ mode: "run", srcDir: "./app", glob: false, }); ``` +Note: The `nwProcess` is a [Node.js process](https://nodejs.org/api/process.html#process) + ### Build Mode Build with defaults: diff --git a/src/run.js b/src/run.js index 649852a7..0ec0f739 100644 --- a/src/run.js +++ b/src/run.js @@ -23,7 +23,7 @@ import util from './util.js'; * @async * @function * @param {RunOptions} options Run mode options - * @returns {Promise} + * @returns {Promise} - A Node.js process object */ async function run({ version = 'latest', @@ -35,6 +35,11 @@ async function run({ glob = false, argv = [], }) { + /** + * @type {child_process.ChildProcess | null} + */ + let nwProcess = null; + try { if (util.EXE_NAME[platform] === undefined) { throw new Error('Unsupported platform.'); @@ -49,8 +54,8 @@ async function run({ ); return new Promise((res, rej) => { - // It is assumed that the package.json is located at srcDir/package.json - const nwProcess = child_process.spawn( + /* It is assumed that the package.json is located at `${options.srcDir}/package.json` */ + nwProcess = child_process.spawn( path.resolve(nwDir, util.EXE_NAME[platform]), [...[srcDir], ...argv], { stdio: 'inherit' }, @@ -68,6 +73,7 @@ async function run({ } catch (error) { console.error(error); } + return nwProcess; } export default run; diff --git a/tests/specs/bld.test.js b/tests/specs/bld.test.js new file mode 100644 index 00000000..f4362837 --- /dev/null +++ b/tests/specs/bld.test.js @@ -0,0 +1,61 @@ +import path from 'node:path'; +import process from 'node:process'; + +import { By } from 'selenium-webdriver'; +import chrome from 'selenium-webdriver/chrome.js'; +import { beforeAll, describe, expect, it } from 'vitest'; + +import build from '../../src/bld.js'; +import get from '../../src/get/index.js'; +import util from '../../src/util.js'; + +const { Driver, ServiceBuilder, Options } = chrome; + +describe.skip('bld test suite', async () => { + let driver = undefined; + + const nwOptions = { + srcDir: 'tests/fixtures/app', + mode: 'build', + version: '0.93.0', + flavor: 'sdk', + platform: util.PLATFORM_KV[process.platform], + arch: util.ARCH_KV[process.arch], + downloadUrl: 'https://dl.nwjs.io', + manifestUrl: 'https://nwjs.io/versions', + outDir: 'tests/fixtures/out/app', + cacheDir: './node_modules/nw', + cache: true, + ffmpeg: false, + glob: false, + managedManifest: false, + nativeAddon: false, + zip: false + }; + + beforeAll(async () => { + await get(nwOptions); + }, Infinity); + + it('builds without errors', async () => { + await build(nwOptions); + }); + + it('runs after build', async () => { + const options = new Options(); + const args = [ + `--nwapp=${path.resolve('test', 'fixture', 'app')}`, + '--headless=new', + ]; + options.addArguments(args); + + const chromedriverPath = util.getPath('chromedriver', nwOptions); + + const service = new ServiceBuilder(chromedriverPath).build(); + + driver = Driver.createSession(options, service); + const text = await driver.findElement(By.id('test')).getText(); + expect(text).toBe('Hello, World!'); + }, { timeout: Infinity }); + +}); diff --git a/tests/specs/osx.test.js b/tests/specs/osx.test.js index 5a15ba21..894fed4b 100644 --- a/tests/specs/osx.test.js +++ b/tests/specs/osx.test.js @@ -14,7 +14,7 @@ import nodeManifest from '../../package.json'; describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function () { const outDir = './tests/fixtures/macos'; - const appPath = path.join(outDir, 'nwapp.app'); + const appPath = path.join(outDir, 'Demo.app'); const releaseInfo = await util.getReleaseInfo( nodeManifest.devDependencies.nw.split('^')[1], util.PLATFORM_KV['darwin'], @@ -30,7 +30,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function 'Versions', chromiumVersion, 'Helpers', - 'nwapp Helper (Alerts).app'); + 'Demo Helper (Alerts).app'); const helperGPUPath = path.join(appPath, 'Contents', 'Frameworks', @@ -38,7 +38,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function 'Versions', chromiumVersion, 'Helpers', - 'nwapp Helper (GPU).app'); + 'Demo Helper (GPU).app'); const helperPluginPath = path.join(appPath, 'Contents', 'Frameworks', @@ -46,7 +46,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function 'Versions', chromiumVersion, 'Helpers', - 'nwapp Helper (Plugin).app'); + 'Demo Helper (Plugin).app'); const helperRendererPath = path.join(appPath, 'Contents', 'Frameworks', @@ -54,7 +54,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function 'Versions', chromiumVersion, 'Helpers', - 'nwapp Helper (Renderer).app'); + 'Demo Helper (Renderer).app'); const helperPath = path.join(appPath, 'Contents', 'Frameworks', @@ -62,7 +62,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function 'Versions', chromiumVersion, 'Helpers', - 'nwapp Helper.app'); + 'Demo Helper.app'); beforeAll(async function () { /* Copy the cached NW.js into a specific `outDir`. */ @@ -72,7 +72,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function /* Rename relevant bundles' plists and executables. */ await setOsxConfig({ app: { - name: 'nwapp', + name: 'Demo', LSApplicationCategoryType: 'public.app-category.utilities', CFBundleIdentifier: 'io.nwutils.demo', CFBundleName: 'Demo', @@ -113,27 +113,27 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function }); it('renames the executables correctly', async function () { - const appExePath = path.join(appPath, 'Contents', 'MacOS', 'nwapp'); + const appExePath = path.join(appPath, 'Contents', 'MacOS', 'Demo'); const appExePathExists = await util.fileExists(appExePath); expect(appExePathExists).toEqual(true); - const helperAlertsExePath = path.join(helperAlertsPath, 'Contents', 'MacOS', 'nwapp Helper (Alerts)'); + const helperAlertsExePath = path.join(helperAlertsPath, 'Contents', 'MacOS', 'Demo Helper (Alerts)'); const helperAlertsExePathExists = await util.fileExists(helperAlertsExePath); expect(helperAlertsExePathExists).toEqual(true); - const helperGPUExePath = path.join(helperGPUPath, 'Contents', 'MacOS', 'nwapp Helper (GPU)'); + const helperGPUExePath = path.join(helperGPUPath, 'Contents', 'MacOS', 'Demo Helper (GPU)'); const helperGPUExePathExists = await util.fileExists(helperGPUExePath); expect(helperGPUExePathExists).toEqual(true); - const helperPluginExePath = path.join(helperPluginPath, 'Contents', 'MacOS', 'nwapp Helper (Plugin)'); + const helperPluginExePath = path.join(helperPluginPath, 'Contents', 'MacOS', 'Demo Helper (Plugin)'); const helperPluginExePathExists = await util.fileExists(helperPluginExePath); expect(helperPluginExePathExists).toEqual(true); - const helperRendererExePath = path.join(helperRendererPath, 'Contents', 'MacOS', 'nwapp Helper (Renderer)'); + const helperRendererExePath = path.join(helperRendererPath, 'Contents', 'MacOS', 'Demo Helper (Renderer)'); const helperRendererExePathExists = await util.fileExists(helperRendererExePath); expect(helperRendererExePathExists).toEqual(true); - const helperExePath = path.join(helperPath, 'Contents', 'MacOS', 'nwapp Helper'); + const helperExePath = path.join(helperPath, 'Contents', 'MacOS', 'Demo Helper'); const helperExePathExists = await util.fileExists(helperExePath); expect(helperExePathExists).toEqual(true); }); @@ -157,7 +157,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function expect(ContentsInfoPlistJson.CFBundleSpokenName).toEqual('Demo'); expect(ContentsInfoPlistJson.CFBundleVersion).toEqual('0.0.0'); expect(ContentsInfoPlistJson.CFBundleShortVersionString).toEqual('0.0.0'); - expect(ContentsInfoPlistJson.CFBundleExecutable).toEqual('nwapp'); + expect(ContentsInfoPlistJson.CFBundleExecutable).toEqual('Demo'); expect(ContentsInfoPlistJson.NSLocalNetworkUsageDescription).toEqual('This test application needs to access the local network for testing purposes.'); const HelperAlertsAppJson = plist.parse( @@ -171,10 +171,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function ) ); - expect(HelperAlertsAppJson.CFBundleDisplayName).toEqual('nwapp Helper (Alerts)'); - expect(HelperAlertsAppJson.CFBundleName).toEqual('nwapp Helper (Alerts)'); + expect(HelperAlertsAppJson.CFBundleDisplayName).toEqual('Demo Helper (Alerts)'); + expect(HelperAlertsAppJson.CFBundleName).toEqual('Demo Helper (Alerts)'); expect(HelperAlertsAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.alert'); - expect(HelperAlertsAppJson.CFBundleExecutable).toEqual('nwapp Helper (Alerts)'); + expect(HelperAlertsAppJson.CFBundleExecutable).toEqual('Demo Helper (Alerts)'); const HelperGpuAppJson = plist.parse( await fs.promises.readFile( @@ -187,10 +187,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function ) ); - expect(HelperGpuAppJson.CFBundleDisplayName).toEqual('nwapp Helper (GPU)'); - expect(HelperGpuAppJson.CFBundleName).toEqual('nwapp Helper (GPU)'); + expect(HelperGpuAppJson.CFBundleDisplayName).toEqual('Demo Helper (GPU)'); + expect(HelperGpuAppJson.CFBundleName).toEqual('Demo Helper (GPU)'); expect(HelperGpuAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.gpu'); - expect(HelperGpuAppJson.CFBundleExecutable).toEqual('nwapp Helper (GPU)'); + expect(HelperGpuAppJson.CFBundleExecutable).toEqual('Demo Helper (GPU)'); const HelperPluginAppJson = plist.parse( await fs.promises.readFile( @@ -203,10 +203,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function ) ); - expect(HelperPluginAppJson.CFBundleDisplayName).toEqual('nwapp Helper (Plugin)'); - expect(HelperPluginAppJson.CFBundleName).toEqual('nwapp Helper (Plugin)'); + expect(HelperPluginAppJson.CFBundleDisplayName).toEqual('Demo Helper (Plugin)'); + expect(HelperPluginAppJson.CFBundleName).toEqual('Demo Helper (Plugin)'); expect(HelperPluginAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.plugin'); - expect(HelperPluginAppJson.CFBundleExecutable).toEqual('nwapp Helper (Plugin)'); + expect(HelperPluginAppJson.CFBundleExecutable).toEqual('Demo Helper (Plugin)'); const HelperRendererAppJson = plist.parse( await fs.promises.readFile( @@ -219,10 +219,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function ) ); - expect(HelperRendererAppJson.CFBundleDisplayName).toEqual('nwapp Helper (Renderer)'); - expect(HelperRendererAppJson.CFBundleName).toEqual('nwapp Helper (Renderer)'); + expect(HelperRendererAppJson.CFBundleDisplayName).toEqual('Demo Helper (Renderer)'); + expect(HelperRendererAppJson.CFBundleName).toEqual('Demo Helper (Renderer)'); expect(HelperRendererAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.renderer'); - expect(HelperRendererAppJson.CFBundleExecutable).toEqual('nwapp Helper (Renderer)'); + expect(HelperRendererAppJson.CFBundleExecutable).toEqual('Demo Helper (Renderer)'); const HelperAppJson = plist.parse( await fs.promises.readFile( @@ -235,10 +235,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function ) ); - expect(HelperAppJson.CFBundleDisplayName).toEqual('nwapp Helper'); - expect(HelperAppJson.CFBundleName).toEqual('nwapp Helper'); + expect(HelperAppJson.CFBundleDisplayName).toEqual('Demo Helper'); + expect(HelperAppJson.CFBundleName).toEqual('Demo Helper'); expect(HelperAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper'); - expect(HelperAppJson.CFBundleExecutable).toEqual('nwapp Helper'); + expect(HelperAppJson.CFBundleExecutable).toEqual('Demo Helper'); afterAll(async function () { await fs.promises.rm('./tests/fixtures/macos', { recursive: true, force: true }); diff --git a/tests/specs/run.test.js b/tests/specs/run.test.js index dbd16fc4..e5fe12e6 100644 --- a/tests/specs/run.test.js +++ b/tests/specs/run.test.js @@ -1,63 +1,41 @@ -import path from 'node:path'; import process from 'node:process'; -import { By } from 'selenium-webdriver'; -import chrome from 'selenium-webdriver/chrome.js'; import { beforeAll, describe, expect, it } from 'vitest'; -import build from '../../src/bld.js'; import get from '../../src/get/index.js'; +import run from '../../src/run.js'; import util from '../../src/util.js'; -const { Driver, ServiceBuilder, Options } = chrome; - -describe.skip('build test suite', async () => { - let driver = undefined; +describe('run test suite', async () => { const nwOptions = { srcDir: 'tests/fixtures/app', mode: 'build', - version: '0.83.0', + version: '0.93.0', flavor: 'sdk', platform: util.PLATFORM_KV[process.platform], arch: util.ARCH_KV[process.arch], downloadUrl: 'https://dl.nwjs.io', manifestUrl: 'https://nwjs.io/versions', outDir: 'tests/fixtures/out/app', - cacheDir: 'tests/fixtures/cache', + cacheDir: './node_modules/nw', cache: true, ffmpeg: false, glob: false, managedManifest: false, nativeAddon: false, - zip: false, - app: { - name: 'demo' - } + zip: false }; beforeAll(async () => { await get(nwOptions); }, Infinity); - it('builds without errors', async () => { - await build(nwOptions); + it.skipIf(process.platform === 'win32')('runs and is killed via code', async () => { + const nwProcess = await run(nwOptions); + if (nwProcess) { + nwProcess.kill(); + expect(nwProcess.killed).toEqual(true); + } }); - - it('runs after build', async () => { - const options = new Options(); - const args = [ - `--nwapp=${path.resolve('test', 'fixture', 'app')}`, - '--headless=new', - ]; - options.addArguments(args); - - const chromedriverPath = util.getPath('chromedriver', nwOptions); - - const service = new ServiceBuilder(chromedriverPath).build(); - - driver = Driver.createSession(options, service); - const text = await driver.findElement(By.id('test')).getText(); - expect(text).toBe('Hello, World!'); - }, { timeout: Infinity }); });