Repository navigation
fix: preserve saved environment and login during upgrade #342
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
Open
Fermionic-Lyu
wants to merge
1
commit into
main
Choose a base branch
from
fix/upgrade-preserve-environment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| import { spawnSync } from 'node:child_process' | ||
| import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { expect, it } from 'vitest' | ||
| import { upgrade } from '../src/commands/upgrade.js' | ||
|
|
||
| const installer = readFileSync(new URL('../install.sh', import.meta.url), 'utf8') | ||
| const environmentStep = installer.slice(installer.indexOf('# ---- environment'), installer.indexOf('# ---- agent setup')) | ||
| const repo = fileURLToPath(new URL('..', import.meta.url)) | ||
|
|
||
| it.skipIf(process.platform === 'win32').each([undefined, 'prod', 'staging'])( | ||
| 'binary upgrade preserves saved environment and login with INSTA_ENV=%s', async (override) => { | ||
| const home = mkdtempSync(join(tmpdir(), 'insta-upgrade-env-')) | ||
| const config = join(home, '.insta', 'config.json') | ||
| const saved = { INSTA_ENV: process.env.INSTA_ENV, INSTA_UPDATE_CACHE: process.env.INSTA_UPDATE_CACHE } | ||
| try { | ||
| mkdirSync(join(home, '.insta')) | ||
| const original = JSON.stringify({ apiUrl: 'https://api.staging.instacloud.com', accessToken: 'test-access', refreshToken: 'test-refresh', user: { id: 'test-user' } }) | ||
| writeFileSync(config, original) | ||
| writeFileSync(join(home, 'insta'), '#!/bin/sh\nexec "$TEST_NODE" --import tsx "$TEST_CLI" "$@"\n', { mode: 0o755 }) | ||
| if (override === undefined) delete process.env.INSTA_ENV | ||
| else process.env.INSTA_ENV = override | ||
| process.env.INSTA_UPDATE_CACHE = join(home, 'update-check.json') | ||
| const runEnvironment = (env: NodeJS.ProcessEnv) => { | ||
| const result = spawnSync('sh', ['-c', `set -eu\n${environmentStep}`], { | ||
| cwd: repo, encoding: 'utf8', timeout: 10_000, | ||
| env: { ...env, HOME: home, INSTALL_DIR: home, BIN: 'insta', ENV_NAME: env.INSTA_ENV ?? '', | ||
| TEST_NODE: process.execPath, TEST_CLI: join(repo, 'src/index.ts'), INSTA_NO_AUTOUPDATE: '1' }, | ||
| }) | ||
| expect(result.status, result.stderr).toBe(0) | ||
| } | ||
| let installs = 0 | ||
| await upgrade('0.0.1', { | ||
| channel: 'binary', latest: '0.0.2', installDir: home, report: () => {}, observe: async () => '0.0.2', | ||
| run: async (spec) => { | ||
| installs++ | ||
| expect(spec.env.INSTA_ENV).toBe(override) | ||
| runEnvironment(spec.env) | ||
| expect(readFileSync(config, 'utf8')).toBe(original) | ||
| if (installs === 1) throw new Error('exercise unpinned fallback') | ||
| }, | ||
| }) | ||
| expect(installs).toBe(2) | ||
| expect(readFileSync(config, 'utf8')).toBe(original) | ||
| runEnvironment({ ...process.env, INSTA_ENV: 'prod', INSTA_UPGRADE: '0' }) | ||
| expect(JSON.parse(readFileSync(config, 'utf8')).apiUrl).toBe('https://api.instacloud.com') | ||
| } finally { | ||
| for (const [key, value] of Object.entries(saved)) { | ||
| if (value === undefined) delete process.env[key] | ||
| else process.env[key] = value | ||
| } | ||
| rmSync(home, { recursive: true, force: true }) | ||
| } | ||
| }, | ||
| ) | ||
Oops, something went wrong.
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.
P2: The spawned real CLI (
src/index.tsvia thehome/instawrapper) runs the full startup path:trackCommandat src/index.ts posts PostHog telemetry for every command and is awaited, andmaybeUpdatecan respawn a detached__update-checkthat hits the npm registry.runEnvironmentdisables auto-update but not telemetry, so the regression suite sends real network requests (and pollutes prod Telemetry with test events), and offline/slow CI can break the 10 sspawnSynctimeout. AddINSTA_NO_TELEMETRY: '1'to the spawned env.Prompt for AI agents