Skip to content

Commit dc03350

Browse files
test: fix e2e imports
1 parent 83e324d commit dc03350

12 files changed

Lines changed: 81 additions & 72 deletions

File tree

.github/workflows/javascript.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
HYPERONE_PASSWORD: ${{secrets.HYPERONE_PASSWORD}}
3737
AWS_SECRET_KEY: ${{secrets.AWS_SECRET_KEY}}
3838
- name: Run e2e tests
39-
run: npm run test
39+
run: yarn run test
4040
working-directory: ./packages/cli-device-node
4141
if: ${{ github.event_name == 'push' }}
4242
- name: Build NodeJS release files

extensions/cli-ext-container-helper/commands/setup.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Command } from '@hyperone/cli-framework';
2-
import fs from 'fs';
3-
import path from 'path';
2+
import path from 'node:path';
3+
import fs from 'node:fs';
4+
import os from 'node:os';
45
import { openapi } from '@hyperone/cli-core';
56

67
const get_config_path = () => {
7-
const docker_config_dir = process.env.DOCKER_CONFIG || path.join(require('os').homedir(), '.docker');
8+
const docker_config_dir = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
89
return path.join(docker_config_dir, 'config.json');
910
};
1011

extensions/cli-ext-root-openapi-generator/middlewares/uri-upload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export default {
3636
if (!value.startsWith('file://')) {
3737
continue;
3838
}
39-
const path = require('path');
39+
4040
const filepath = new URL(value).pathname;
41-
const filename = path.basename(filepath);
41+
const filename = filepath.split('/').pop();
4242
const filestream = device.createReadStream(filepath);
4343
const filestat = await device.statFile(filepath);
4444
const filesize = filestat.size;

packages/cli-device-node/lib/tests.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
const shlex = require('shlex');
2-
const child_process = require('child_process');
3-
const path = require('path');
4-
const fs = require('fs').promises;
5-
const { createWriteStream } = require('fs');
6-
const os = require('os');
7-
const util = require('util');
8-
const crypto = require('crypto');
9-
const randomBytes = util.promisify(crypto.randomBytes);
10-
const pty = require('node-pty');
11-
12-
const randomToken = (len = 16) => randomBytes(len).then(x => x.toString('hex'));
1+
import shlex from 'shlex';
2+
import pty from 'node-pty';
3+
4+
import { promises as fs, createWriteStream } from 'node:fs';
5+
import { randomBytes, createHash } from 'node:crypto';
6+
import { fileURLToPath } from 'node:url';
7+
8+
import child_process from 'node:child_process';
9+
import path from 'node:path';
10+
import os from 'node:os';
11+
12+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
13+
14+
const randomToken = async (len = 16) => randomBytes(len).toString('hex');
1315

1416
const run = (cmd, options = {}) => new Promise((resolve, reject) => {
1517
if (process.argv.includes('--serial')) {
@@ -18,9 +20,16 @@ const run = (cmd, options = {}) => new Promise((resolve, reject) => {
1820
}
1921

2022
const args = [
21-
path.join(__dirname, '../dist/h1.js'),
23+
path.join(__dirname, '../bin/h1.js'),
2224
...shlex.split(cmd).slice(1),
2325
];
26+
options.env = {
27+
...options.env,
28+
//TODO remove when it becomes stable
29+
// "Importing JSON modules is an experimental feature" - https://nodejs.org/api/esm.html#json-modules
30+
// "The Fetch API is an experimental feature" - https://nodejs.org/api/globals.html#fetch
31+
NODE_NO_WARNINGS: '1'
32+
};
2433
const program = child_process.spawn(process.argv[0], args, options);
2534
const chunks = [];
2635

@@ -57,7 +66,7 @@ const runJson = async (cmd, options = {}) => {
5766

5867
const runPty = async (cmd, inputs, options = {}) => new Promise((resolve, reject) => {
5968
const ptyProcess = pty.spawn(process.argv[0], [
60-
path.join(__dirname, '../dist/h1.js'),
69+
path.join(__dirname, '../bin/h1.js'),
6170
...shlex.split(cmd).slice(1),
6271
], options);
6372
const chunks = [];
@@ -126,19 +135,18 @@ const getName = (...names) => [...names, Date.now().toString()]
126135
.replace(/[^a-zA-Z0-9]/g, '-');
127136

128137
const downloadCachedFile = url => new Promise((resolve, reject) => {
129-
const suffix = crypto.createHash('sha256').update(url).digest('hex');
138+
const suffix = createHash('sha256').update(url).digest('hex');
130139
const filename = path.join(os.tmpdir(), `test-h1-cli-v2-${suffix}`);
131-
const stream = createWriteStream(filename);
132-
stream.on('finish', () => resolve(filename));
140+
133141
fetch(url)
134-
.then(resp => resp.body
135-
.pipe(stream)
136-
.on('error', reject)
137-
)
138-
.catch(reject);
142+
.then(response => response.blob())
143+
.then(blob => fs.writeFile(filename, blob.stream()))
144+
.catch(reject)
145+
.then(() => resolve(filename))
146+
;
139147
});
140148

141-
module.exports = {
149+
export {
142150
run,
143151
runJson,
144152
runPty,

packages/cli-device-node/scripts/validate-ext.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/* eslint-disable no-console */
2+
import info from '../package.json' assert { type: 'json' };
23

3-
import packageInfo from './../package.json';
4-
5-
for (const pkg of Object.keys(packageInfo.dependencies).filter(x => x.startsWith('@hyperone/cli-ext'))) {
4+
for (const pkg of Object.keys(info.dependencies).filter(x => x.startsWith('@hyperone/cli-ext'))) {
65
try {
7-
require(pkg);
6+
await import(pkg);
87
console.log('Successfully loaded', pkg);
98
} catch (err) {
109
console.log('Failed to load', pkg);

packages/cli-device-node/tests/auth.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
const ava = require('ava');
2-
const { run, runJson, runPty, withTemp, withVariable } = require('./../lib/tests');
1+
import test from 'ava';
2+
import { run, runJson, runPty, withTemp, withVariable } from '../lib/tests.js';
33

4-
ava('display user profile', async t => {
4+
test('display user profile', async t => {
55
const output = await runJson('h1 auth me');
66
t.true(output.aud.includes('https://api.hyperone.com/v2'));
77
});
88

9-
ava('auth user', withTemp(withVariable(['username', 'password'], async (t, tmpDir, username, password) => {
9+
test('auth user', withTemp(withVariable(['username', 'password'], async (t, tmpDir, username, password) => {
1010
const options = { env: { HOME: tmpDir } };
1111
const output = await run(`h1 auth user --username ${username} --password ${password}`, options);
1212
t.true(output.includes('Token successfully updated.'));
1313
const me = await runJson('h1 auth me', options);
1414
t.true(me.sub.includes(username));
1515
})));
1616

17-
ava('auth aws', withTemp(withVariable(['aws-access-key', 'aws-secret-key'], async (t, tmpDir, access_key, secret_key) => {
17+
test('auth aws', withTemp(withVariable(['aws-access-key', 'aws-secret-key'], async (t, tmpDir, access_key, secret_key) => {
1818
const options = { env: { HOME: tmpDir } };
1919
const output = await run(`h1 auth aws --access-key-id ${access_key} --secret-access-key ${secret_key}`, options);
2020
t.true(output.includes('Token successfully updated.'));
2121
const me = await runJson('h1 auth me', options);
2222
t.true(me.sub.includes('https://sts.amazonaws.com/'));
2323
})));
2424

25-
ava('auth user interactive', withTemp(withVariable(['username', 'password'], async (t, tmpDir, username, password) => {
25+
test('auth user interactive', withTemp(withVariable(['username', 'password'], async (t, tmpDir, username, password) => {
2626
const options = { env: { HOME: tmpDir } };
2727
const output = await runPty('h1 auth user', [username, password].map(x => `${x}\r`), options);
2828
t.true(output.includes('Token successfully updated.'));

packages/cli-device-node/tests/config-autocomplete.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const ava = require('ava');
2-
const { run, withTemp } = require('../lib/tests');
1+
import test from 'ava';
2+
import { run, withTemp } from '../lib/tests.js';
33

44
const comply = async (cmd) => {
55
const output = await run(`h1 config autocomplete comply --cmd '${cmd}'`);
66
return output.split('\n');
77
};
88

9-
ava('autocomplete comply', async t => {
9+
test('autocomplete comply', async t => {
1010
const category = await comply('h1 i');
1111
t.true(category.includes('iam'));
1212

@@ -18,7 +18,7 @@ ava('autocomplete comply', async t => {
1818
t.true(option.includes('--project'));
1919
});
2020

21-
ava('autocomplete install', withTemp(async (t, tempDir) => {
21+
test('autocomplete install', withTemp(async (t, tempDir) => {
2222
const options = {env: {HOME: tempDir}};
2323
const output = await run('h1 config autocomplete install', options);
2424
t.true(output.includes(tempDir));

packages/cli-device-node/tests/config-settings.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const ava = require('ava');
2-
const { run, runJson, randomToken } = require('./../lib/tests');
1+
import test from 'ava';
2+
import { run, runJson, randomToken } from '../lib/tests.js';
33

4-
ava('config settings set', async t => {
4+
test('config settings set', async t => {
55
const output = await run('h1 config settings set --key x --value x');
66
t.true(output.includes('Configuration option set'));
77
});
88

9-
ava('config settings lifecycle', async t => {
9+
test('config settings lifecycle', async t => {
1010
const token = await randomToken();
1111
const key = 'token-lifecycle';
1212
const output_set = await run(`h1 config settings set --key ${key} --value ${token}`);

packages/cli-device-node/tests/container-helper.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const ava = require('ava');
2-
const { run, runJson, withTemp, withVariable } = require('../lib/tests');
3-
const util = require('util');
4-
const exec = util.promisify(require('child_process').exec);
1+
import test from 'ava';
2+
import { run, runJson, withTemp, withVariable } from '../lib/tests.js';
3+
import { promisify } from 'node:util';
4+
import child_process from 'node:child_process';
5+
const exec = promisify(child_process.exec);
56

67
// test have known isolation issue on CI
7-
const skipIfCi = process.env.CI ? ava.skip : ava;
8+
const skipIfCi = process.env.CI ? test.skip : test;
89

910
skipIfCi('container helper lifecycle', withVariable(['project'], withTemp(async (t, project, tmpDir) => {
1011
const options = {

packages/cli-device-node/tests/core.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const ava = require('ava');
2-
const { run } = require('./../lib/tests');
1+
import test from 'ava';
2+
import { run } from '../lib/tests.js';
33

4-
ava('display version option output', async t => {
4+
test('display version option output', async t => {
55
const output = await run('h1 -v');
66
t.true(output.includes('h1 version 2'));
77
});
88

9-
ava('display category', async t => {
9+
test('display category', async t => {
1010
const output = await run('h1 iam');
1111
t.true(output.includes('Management of project resource'));
1212
});
1313

14-
ava('display command help', async t => {
14+
test('display command help', async t => {
1515
const output = await run('h1 iam project create --help');
1616
t.true(output.includes('Show help message and exit.'));
1717
});
1818

19-
ava('display error for missing parameter', async t => {
19+
test('display error for missing parameter', async t => {
2020
const err = await t.throwsAsync(run('h1 config settings set'));
2121
t.true(err.output.includes('--key'));
2222
t.true(err.output.includes('--value'));

0 commit comments

Comments
 (0)