-
Notifications
You must be signed in to change notification settings - Fork 8
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
feat: allow setting environment variables and disable extending in DenoServer #82
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2206898
feat: allow setting environment variables and disable extending in De…
danez a7aab67
chore: fix test
danez 80b52fd
chore: fix tests
danez 4037410
chore: add test
danez 8893344
chore: debug
danez 231d3a4
chore: fix tests
danez c7a3911
chore: fix tests?
danez ea134f3
Update test/bridge.ts
danez 4eedc72
Merge branch 'main' into environment
danez f6fe66a
Update src/server/server.ts
danez 46f5999
Merge branch 'main' into environment
danez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Buffer } from 'buffer' | ||
import fs from 'fs' | ||
import { createRequire } from 'module' | ||
import { platform, env } from 'process' | ||
import { PassThrough } from 'stream' | ||
|
||
import test from 'ava' | ||
import nock from 'nock' | ||
import { spy } from 'sinon' | ||
import tmp, { DirectoryResult } from 'tmp-promise' | ||
|
||
import { DenoBridge } from '../src/bridge.js' | ||
import { getPlatformTarget } from '../src/platform.js' | ||
|
||
const require = createRequire(import.meta.url) | ||
const archiver = require('archiver') | ||
|
||
const getMockDenoBridge = function (tmpDir: DirectoryResult, mockBinaryOutput: string) { | ||
const latestVersion = '1.20.3' | ||
const data = new PassThrough() | ||
const archive = archiver('zip', { zlib: { level: 9 } }) | ||
|
||
archive.pipe(data) | ||
archive.append(Buffer.from(mockBinaryOutput.replace(/@@@latestVersion@@@/g, latestVersion)), { | ||
name: platform === 'win32' ? 'deno.exe' : 'deno', | ||
}) | ||
archive.finalize() | ||
|
||
const target = getPlatformTarget() | ||
|
||
nock('https://dl.deno.land').get('/release-latest.txt').reply(200, `v${latestVersion}`) | ||
nock('https://dl.deno.land') | ||
.get(`/release/v${latestVersion}/deno-${target}.zip`) | ||
.reply(200, () => data) | ||
|
||
const beforeDownload = spy() | ||
const afterDownload = spy() | ||
|
||
return new DenoBridge({ | ||
cacheDirectory: tmpDir.path, | ||
onBeforeDownload: beforeDownload, | ||
onAfterDownload: afterDownload, | ||
useGlobal: false, | ||
}) | ||
} | ||
|
||
test.serial('Does not inherit environment variables if `extendEnv` is false', async (t) => { | ||
const tmpDir = await tmp.dir() | ||
const deno = getMockDenoBridge( | ||
tmpDir, | ||
`#!/usr/bin/env sh | ||
|
||
if [ "$1" = "test" ] | ||
then | ||
env | ||
else | ||
echo "deno @@@latestVersion@@@" | ||
fi`, | ||
) | ||
|
||
// The environment sets some variables so let us see what they are and remove them from the result | ||
const referenceOutput = await deno.run(['test'], { env: {}, extendEnv: false }) | ||
env.TADA = 'TUDU' | ||
const result = await deno.run(['test'], { env: { LULU: 'LALA' }, extendEnv: false }) | ||
let output = result?.stdout ?? '' | ||
|
||
delete env.TADA | ||
|
||
referenceOutput?.stdout.split('\n').forEach((line) => { | ||
output = output.replace(line.trim(), '') | ||
}) | ||
output = output.trim().replace(/\n+/, '\n') | ||
|
||
t.is(output, 'LULU=LALA') | ||
|
||
await fs.promises.rmdir(tmpDir.path, { recursive: true }) | ||
}) | ||
|
||
test.serial('Does inherit environment variables if `extendEnv` is true', async (t) => { | ||
const tmpDir = await tmp.dir() | ||
const deno = getMockDenoBridge( | ||
tmpDir, | ||
`#!/usr/bin/env sh | ||
|
||
if [ "$1" = "test" ] | ||
then | ||
env | ||
else | ||
echo "deno @@@latestVersion@@@" | ||
fi`, | ||
) | ||
|
||
// The environment sets some variables so let us see what they are and remove them from the result | ||
const referenceOutput = await deno.run(['test'], { env: {}, extendEnv: true }) | ||
env.TADA = 'TUDU' | ||
const result = await deno.run(['test'], { env: { LULU: 'LALA' }, extendEnv: true }) | ||
let output = result?.stdout ?? '' | ||
|
||
delete env.TADA | ||
|
||
referenceOutput?.stdout.split('\n').forEach((line) => { | ||
output = output.replace(line.trim(), '') | ||
}) | ||
output = output.trim().replace(/\n+/, '\n') | ||
|
||
t.is(output, 'LULU=LALA\nTADA=TUDU') | ||
|
||
await fs.promises.rmdir(tmpDir.path, { recursive: true }) | ||
}) | ||
|
||
test.serial('Does inherit environment variables if `extendEnv` is not set', async (t) => { | ||
const tmpDir = await tmp.dir() | ||
const deno = getMockDenoBridge( | ||
tmpDir, | ||
`#!/usr/bin/env sh | ||
|
||
if [ "$1" = "test" ] | ||
then | ||
env | ||
else | ||
echo "deno @@@latestVersion@@@" | ||
fi`, | ||
) | ||
|
||
// The environment sets some variables so let us see what they are and remove them from the result | ||
const referenceOutput = await deno.run(['test'], { env: {}, extendEnv: true }) | ||
env.TADA = 'TUDU' | ||
const result = await deno.run(['test'], { env: { LULU: 'LALA' } }) | ||
let output = result?.stdout ?? '' | ||
|
||
delete env.TADA | ||
|
||
referenceOutput?.stdout.split('\n').forEach((line) => { | ||
output = output.replace(line.trim(), '') | ||
}) | ||
output = output.trim().replace(/\n+/, '\n') | ||
|
||
t.is(output, 'LULU=LALA\nTADA=TUDU') | ||
|
||
await fs.promises.rmdir(tmpDir.path, { recursive: true }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what case would we set
extendEnv
totrue
? Or is this for backwards compatibility?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value is
true
atm. I was hardcoding this at first, but then wondered ifrunInBackground
might be used in other scenarios in the future. Do you think I should hardcode this to false?And because
run
does the same, besides the background part I also added the options there.