Skip to content

Commit

Permalink
Fix executable JavaScript scripts running in arbitrary directories
Browse files Browse the repository at this point in the history
It was working inside a Node project with type: module in its
package.json (e.g. Ursa), but not elsewhere.

Also add a test for both types of executable script.
  • Loading branch information
rrthomas committed Jul 25, 2024
1 parent 720479f commit f0845c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/testutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import {compile as ursaCompile} from './ursa/compiler.js'
import {format} from './ursa/fmt.js'
import version from './version.js'

const command = process.env.NODE_ENV === 'coverage' ? './bin/test-run.sh' : './bin/run.js'
export const ursaCommand = process.env.NODE_ENV === 'coverage' ? './bin/test-run.sh' : './bin/run.js'

const arkTargets = new Set(['ark', 'js'])

export function run(args: string[], options: ExecaOptions) {
if (process.env.DEBUG) {
console.log(`run ${command} ${args} ${options.inputFile}`)
console.log(`run ${ursaCommand} ${args} ${options.inputFile}`)
}
return execa(command, args, options)
return execa(ursaCommand, args, options)
}

function arkCompile(source: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/ursa/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ async function compileCommand(args: Args) {
output += serializeVal(exp)
} else {
if (args.executable) {
output += '#!/usr/bin/env -S node --no-warnings --\n'
output += '#!/usr/bin/env -S node --experimental-default-type=module --\n'
output += await buildRuntime()
// Read prelude but elide the "use strict" line.
const prelude = preludeJs.slice(preludeJs.indexOf('\n') + 1)
Expand Down
22 changes: 21 additions & 1 deletion src/ursa/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

import assert from 'assert'
import test from 'ava'
import {execa} from 'execa'
import fs from 'fs'
import kill from 'tree-kill'
import tmp from 'tmp'

import {ursaTest, ursaDirTest, run} from '../testutil.js'
import {
ursaTest, ursaDirTest, ursaCommand, run,
} from '../testutil.js'

[
['Increment a variable in a loop', 'test/increment-variable-in-loop'],
Expand Down Expand Up @@ -69,6 +74,21 @@ ursaDirTest('fs', 'test/fs', 'test/fs.result')
// Rosetta code examples with command-line arguments
ursaTest('Anagrams', 'rosettacode/Anagrams', ['rosettacode/unixdict.txt'])

// Test executable script generation
test('Executable scripts', async (t) => {
for (const target of ['ark', 'js']) {
const tempExecFile = tmp.fileSync({discardDescriptor: true})
// t.teardown(() => tempExecFile.removeCallback())
await execa(ursaCommand, [
`--target=${target}`,
'compile', '--executable', 'test/advent-of-code-2023-day-25.ursa',
`--output=${tempExecFile.name}`,
])
const {stdout} = await execa(tempExecFile.name, ['test/advent-of-code-2023-day-25-input.txt'])
t.deepEqual(stdout, fs.readFileSync('test/advent-of-code-2023-day-25.stdout', {encoding: 'utf-8'}))
}
})

// Complex tests
test('Web server', async (t) => {
const proc = run(['./test/web-server.ursa'], {
Expand Down

0 comments on commit f0845c2

Please sign in to comment.