From 75a9fc02a2aeb91fc1a7b49700fb9617ff3c020b Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sun, 14 Apr 2024 14:37:06 +0530 Subject: [PATCH] fix(findpath): do not look for symlink (#147) On Windows, the symlink is not created if user does not have Administrator privileges. Look for actual file path instead of symlink. --- .github/dependabot.yml | 11 ++++++++++- .github/workflows/ci.yml | 6 +++--- CHANGELOG.md | 12 ++++++++++++ src/util.js | 19 ++++++++----------- test/findpath.test.js | 13 +++++++++++++ test/index.test.js | 8 -------- test/selenium.test.js | 38 +++++++++++++++++++++++--------------- 7 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 test/findpath.test.js delete mode 100644 test/index.test.js diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 06f9a85..adb4492 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,10 +1,19 @@ version: 2 updates: - package-ecosystem: "npm" - directory: "/" + directory: "." schedule: interval: "daily" + versioning-strategy: increase + groups: + npm: + patterns: + - "*" - package-ecosystem: "github-actions" directory: ".github/workflows" schedule: interval: "daily" + groups: + gha: + patterns: + - "*" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5f03619..373f6f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ concurrency: cancel-in-progress: true jobs: - e2e: + test: strategy: matrix: os: [macos-13, ubuntu-22.04, windows-2022] @@ -29,5 +29,5 @@ jobs: run: corepack enable - name: Install dependencies run: npm ci --nwjs-build-type=sdk - - name: Run test - run: npm test + - name: Run tests + run: npm t diff --git a/CHANGELOG.md b/CHANGELOG.md index c48d344..08dad19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Changed + +- `findpath` looks for actual path and not symlink. On Windows, if user does not have Administrator privileges, symlink is not created, then `findpath` fails to find the file path. + +- `findpath` is async with return type `Promise`. +- `findpath` has a secondary options argument. This is useful when trying to get the file path to ChromeDriver: + +```js +const nwPath = await findpath('chromedriver', { flavor: 'sdk' }); +``` + + ## [0.86.0-2] ### Changed diff --git a/src/util.js b/src/util.js index 1deb747..c8bdd68 100644 --- a/src/util.js +++ b/src/util.js @@ -3,6 +3,8 @@ import path from 'node:path'; import process from 'node:process'; import url from 'node:url'; +import parse from './parse.js'; + const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); /** @@ -43,10 +45,12 @@ const EXE_NAME = { * Get the platform dependant path of the NW.js or ChromeDriver binary. * * @param {'nwjs' | 'chromedriver'} executable Path to NW.js or Chromedriver executable. - * @return {string} + * @return {Promise} */ -function findpath(executable = 'nwjs') { - const nwDir = path.resolve(__dirname, '..', 'nwjs'); +async function findpath(executable = 'nwjs', options = {}) { + options = await parse(options); + const nwDir = path.resolve(__dirname, '..', `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`); + /** * File path to executable. * @@ -54,18 +58,11 @@ function findpath(executable = 'nwjs') { */ let binPath = ''; - /** - * Host operating system - * - * @type {NodeJS.Platform} - */ - let hostOs = PLATFORM_KV[process.env.npm_config_nwjs_platform || process.env.NWJS_PLATFORM || process.platform]; - /** * Get the platform dependant path of the NW.js binary. */ function findNwjs() { - binPath = path.resolve(nwDir, EXE_NAME[hostOs]); + binPath = path.resolve(nwDir, EXE_NAME[options.platform]); } /** diff --git a/test/findpath.test.js b/test/findpath.test.js new file mode 100644 index 0000000..1dbc621 --- /dev/null +++ b/test/findpath.test.js @@ -0,0 +1,13 @@ +import fs from 'node:fs'; +import { expect, test } from 'vitest'; + +import util from '../src/util.js'; + +test('nwjs has downloaded and been extracted', function () { + util.findpath('nwjs').then(function (path) { + expect(fs.existsSync(path)).toEqual(true); + }) + .catch((error) => { + console.log(error); + }); +}); diff --git a/test/index.test.js b/test/index.test.js deleted file mode 100644 index 2044701..0000000 --- a/test/index.test.js +++ /dev/null @@ -1,8 +0,0 @@ -import fs from 'node:fs'; -import { expect, test } from 'vitest'; - -import util from '../src/util.js'; - -test('nwjs has downloaded and been extracted', function() { - expect(fs.existsSync(util.findpath())).toBe(true); -}); diff --git a/test/selenium.test.js b/test/selenium.test.js index e3fba08..e00503d 100644 --- a/test/selenium.test.js +++ b/test/selenium.test.js @@ -1,31 +1,39 @@ -import assert from "node:assert"; import path from "node:path"; -import { By } from "selenium-webdriver"; +import selenium from "selenium-webdriver"; import chrome from "selenium-webdriver/chrome.js"; -import { describe, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import util from "../src/util.js"; -const { Driver, ServiceBuilder, Options } = chrome; - -describe("run", async () => { +describe("run", async function () { let driver = undefined; - it("should run post install", async () => { - const options = new Options(); - const args = [ + beforeAll(async function () { + const options = new chrome.Options(); + const seleniumArgs = [ `--nwapp=${path.resolve("test", "app")}`, "--headless=new", ]; - options.addArguments(args); - const chromedriverPath = util.findpath("chromedriver"); + options.addArguments(seleniumArgs); + + const chromedriverPath = await util.findpath("chromedriver", { flavor: 'sdk' }); + const service = new chrome.ServiceBuilder(chromedriverPath).build(); + + driver = chrome.Driver.createSession(options, service); + }); - const service = new ServiceBuilder(chromedriverPath).build(); + it("should run post install", async function () { + const textElement = await driver.findElement(selenium.By.id('test')); - driver = Driver.createSession(options, service); - const text = await driver.findElement(By.id("test")).getText(); - assert.strictEqual(text, "Hello, World! Is anyone out there?"); + const text = await textElement.getText(); + + expect(text).toEqual('Hello, World! Is anyone out there?'); }, { timeout: Infinity }); + + afterAll(function () { + driver.quit(); + }); + });