Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve IP #87

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
#!/usr/bin/env -S node --dns-result-order ipv4first

import yargs from 'yargs'
import { commands } from './commands/index.js'
Expand Down
130 changes: 39 additions & 91 deletions spec/tests/exec.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,48 @@
import { test } from 'tape'
import yargs from 'yargs'
import * as exec from '../../commands/exec.js'
import { runPipe } from '../test.js'

const parser = yargs().scriptName('xst').command(exec).help().fail(false)

const execCmd = async (cmd, args) => {
return await yargs()
.scriptName('xst')
.command(cmd)
.fail(false)
.parse(args)
}

test('shows help', async function (t) {
// Run the command module with --help as argument
const output = await new Promise((resolve, reject) => {
parser.parse(['execute', '--help'], (err, argv, output) => {
if (err) { return reject(err) }
resolve(output)
})
})
const firstLine = output.split('\n')[0]
import { run, runPipe } from '../test.js'

test("calling 'xst execute --help'", async (t) => {
const { stderr, stdout } = await run('xst', ['execute', '--help'])
if (stderr) { return t.fail(stderr) }

const firstLine = stdout.split('\n')[0]

t.equal(firstLine, 'xst execute [<query>] [options]', firstLine)
t.end()
})

test('executes command', async function (t) {
const argv = await new Promise((resolve, reject) => {
parser.parse(['execute', '1+1'], (err, argv, output) => {
if (err) { return reject(err) }
resolve(argv)
})
})
test('executes command', async (t) => {
const { stderr, stdout } = await run('xst', ['execute', '1+1'])
if (stderr) { return t.fail(stderr) }

t.equal(argv.query, '1+1')
t.equal(stdout, '2\n')
})

test('executes command with alias \'exec\'', async function (t) {
const argv = await new Promise((resolve, reject) => {
parser.parse(['exec', '1+1'], (err, argv, output) => {
if (err) { return reject(err) }
resolve(argv)
})
})

t.equal(argv.query, '1+1')
const { stderr, stdout } = await run('xst', ['exec', '1+1'])
if (stderr) { return t.fail(stderr) }

t.equal(stdout, '2\n')
})

test('executes command with alias \'run\'', async function (t) {
const argv = await new Promise((resolve, reject) => {
parser.parse(['run', '1+1'], (err, argv, output) => {
if (err) { return reject(err) }
resolve(argv)
})
})

t.equal(argv.query, '1+1')
const { stderr, stdout } = await run('xst', ['run', '1+1'])
if (stderr) { return t.fail(stderr) }

t.equal(stdout, '2\n')
})

test('executes bound command', async function (t) {
const argv = await new Promise((resolve, reject) => {
parser.parse(['exec', '-b', '{"a":1}', '$a+$a'], (err, argv, output) => {
if (err) { return reject(err) }
resolve(argv)
})
})
t.plan(2)
t.equal(argv.query, '$a+$a')
t.equal(argv.bind.a, 1)
const { stderr, stdout } = await run('xst', ['execute', '-b', '{"a":1}', '$a+$a'])
if (stderr) { return t.fail(stderr) }

t.equal(stdout, '2\n')
})

test('bind parse error', async function (t) {
try {
const res = await execCmd(exec, ['exec', '-b', '{a:1}', '$a+$a'])
t.notOk(res)
} catch (e) {
t.ok(e, e)
}
const { stderr, stdout } = await run('xst', ['execute', '-b', '{a:1}', '$a+$a'])
t.notOk(stdout)
t.ok(stderr, stderr)
})

test('read bind from stdin', async function (t) {
Expand All @@ -87,44 +52,27 @@ test('read bind from stdin', async function (t) {
})

test('cannot read bind from stdin', async function (t) {
try {
const res = await execCmd(exec, ['exec', '-b', '-', '$a+$a'])
t.fail(res)
} catch (e) {
t.ok(e, e)
}
const { stderr, stdout } = await run('xst', ['execute', '-b', '-', '$a+$a'])
if (stdout) { return t.fail(stdout) }
t.ok(stderr, stderr)
})

test('cannot read query file from stdin', async function (t) {
try {
const res = await execCmd(exec, ['exec', '-f', '-'])
t.fail(res)
} catch (e) {
t.ok(e, e)
}
const { stderr, stdout } = await run('xst', ['execute', '-f', '-'])
if (stdout) { return t.fail(stdout) }
t.ok(stderr, stderr)
})

test('read query file', async function (t) {
try {
const argv = await new Promise((resolve, reject) => {
parser.parse(['exec', '-f', './spec/fixtures/test.xq'], (err, argv, output) => {
if (err) { return reject(err) }
resolve(argv)
})
})
t.equals(argv.f, 'spec/fixtures/test.xq', 'should be normalized')
} catch (e) {
t.notOk(e, e)
}
const { stderr, stdout } = await run('xst', ['execute', '-f', './spec/fixtures/test.xq'])
if (stderr) { return t.fail(stderr) }
t.ok(stdout, stdout)
})

test('read query file with query', async function (t) {
try {
const argv = await parser.parse(['exec', '-f', 'spec/fixtures/test.xq', '1+1'])
t.fail(argv, 'Should not return a result')
} catch (e) {
t.ok(e, e)
}
const { stderr, stdout } = await run('xst', ['execute', '-f', './spec/fixtures/test.xq', '1+1'])
if (stdout) { return t.fail(stdout) }
t.ok(stderr, stderr)
})

test('read file from stdin', async function (t) {
Expand Down
2 changes: 1 addition & 1 deletion spec/tests/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ test('with fixtures uploaded', async (t) => {
/db/list-test/tests/info.js
/db/list-test/tests/cli.js
/db/list-test/tests/upload.js
/db/list-test/tests/configuration.js
/db/list-test/tests/exec.js
/db/list-test/tests/configuration.js
/db/list-test/tests/rm.js
/db/list-test/tests/get.js
/db/list-test/tests/list.js
Expand Down