forked from facebookarchive/WebDriverAgent
-
Notifications
You must be signed in to change notification settings - Fork 627
fix: remaining strong box file #1255
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
Merged
+150
−28
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,136 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import {mkdtemp, readFile, rm} from 'node:fs/promises'; | ||
| import {tmpdir} from 'node:os'; | ||
| import path from 'node:path'; | ||
| import {afterEach, beforeEach, describe, it, mock} from 'node:test'; | ||
|
|
||
| import {strongbox} from '@appium/strongbox'; | ||
| import {fs} from '@appium/support'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| import {BOOTSTRAP_PATH} from '../../lib/utils/index.js'; | ||
|
|
||
| let container: string; | ||
| function isolatedStrongbox(name: string) { | ||
| const box = strongbox(name); | ||
| // Preserve the temporary path verbatim instead of Strongbox's container slugification. | ||
| Object.defineProperty(box, 'container', {value: container}); | ||
| return box; | ||
| } | ||
| mock.module('@appium/strongbox', { | ||
| namedExports: { | ||
| strongbox: isolatedStrongbox, | ||
| }, | ||
| }); | ||
| const {WebDriverAgent} = await import('../../lib/webdriveragent.js'); | ||
| const packageInfo = JSON.parse(await readFile(path.join(BOOTSTRAP_PATH, 'package.json'), 'utf8')); | ||
| const itemName = 'recentWdaModuleVersion'; | ||
|
|
||
| describe('WDA project cleanup persistence', function () { | ||
| let sandbox: sinon.SinonSandbox; | ||
| let legacyMarker: sinon.SinonStub; | ||
|
|
||
| beforeEach(async function () { | ||
| container = await mkdtemp(path.join(tmpdir(), 'wda-cleanup-')); | ||
| sandbox = sinon.createSandbox(); | ||
| legacyMarker = sandbox | ||
| .stub(fs, 'exists') | ||
| .withArgs(path.resolve(process.env.HOME ?? '', '.appium', 'webdriveragent', 'upgrade.time')) | ||
| .resolves(false); | ||
| }); | ||
|
|
||
| afterEach(async function () { | ||
| sandbox.restore(); | ||
| await rm(container, {recursive: true, force: true}); | ||
| }); | ||
|
|
||
| function newAgent() { | ||
| const agent = new WebDriverAgent({device: {udid: 'test-udid'}, platformVersion: '17.2'}); | ||
| const clean = sandbox.stub(agent.xcodebuild, 'cleanProject').resolves(); | ||
| return {clean, run: async () => await (agent as any)._cleanupProjectIfFresh()}; | ||
| } | ||
|
|
||
| async function persist(version: string) { | ||
| await isolatedStrongbox(packageInfo.name).createItemWithValue(itemName, version); | ||
| } | ||
|
|
||
| async function persistedVersion() { | ||
| return (await isolatedStrongbox(packageInfo.name).createItem<string>(itemName)).value; | ||
| } | ||
|
KazuCocoa marked this conversation as resolved.
|
||
|
|
||
| it('reuses the persisted version despite a legacy marker across new agents', async function () { | ||
| legacyMarker.resolves(true); | ||
| await persist(packageInfo.version); | ||
| for (let i = 0; i < 2; i++) { | ||
| const agent = newAgent(); | ||
| await agent.run(); | ||
| sandbox.assert.notCalled(agent.clean); | ||
| } | ||
| sandbox.assert.notCalled(legacyMarker); | ||
| assert.equal(await persistedVersion(), packageInfo.version); | ||
| }); | ||
|
|
||
| it('cleans an older persisted version once without a legacy marker', async function () { | ||
| await persist('5.0.0'); | ||
| const first = newAgent(); | ||
| await first.run(); | ||
| sandbox.assert.calledOnce(first.clean); | ||
| assert.equal(await persistedVersion(), packageInfo.version); | ||
| const second = newAgent(); | ||
| await second.run(); | ||
| sandbox.assert.notCalled(second.clean); | ||
| sandbox.assert.notCalled(legacyMarker); | ||
| }); | ||
|
|
||
| it('initializes missing version state without consulting a legacy marker', async function () { | ||
| legacyMarker.resolves(true); | ||
| const first = newAgent(); | ||
| await first.run(); | ||
| sandbox.assert.notCalled(first.clean); | ||
| assert.equal(await persistedVersion(), packageInfo.version); | ||
| const second = newAgent(); | ||
| await second.run(); | ||
| sandbox.assert.notCalled(second.clean); | ||
| sandbox.assert.notCalled(legacyMarker); | ||
| }); | ||
|
|
||
| it('initializes a fresh installation without cleaning', async function () { | ||
| for (let i = 0; i < 2; i++) { | ||
| const agent = newAgent(); | ||
| await agent.run(); | ||
| sandbox.assert.notCalled(agent.clean); | ||
| } | ||
| sandbox.assert.notCalled(legacyMarker); | ||
| assert.equal(await persistedVersion(), packageInfo.version); | ||
| }); | ||
|
|
||
| it('preserves a newer persisted version', async function () { | ||
| await persist('999.0.0'); | ||
| const agent = newAgent(); | ||
| await agent.run(); | ||
| sandbox.assert.notCalled(agent.clean); | ||
| assert.equal(await persistedVersion(), '999.0.0'); | ||
| }); | ||
|
|
||
| it('repairs a damaged persisted version without treating it as legacy state', async function () { | ||
| legacyMarker.resolves(true); | ||
| await persist('not-a-version'); | ||
| const agent = newAgent(); | ||
| await agent.run(); | ||
| sandbox.assert.notCalled(agent.clean); | ||
| sandbox.assert.notCalled(legacyMarker); | ||
| assert.equal(await persistedVersion(), packageInfo.version); | ||
| }); | ||
|
|
||
| it('retries cleanup after a failure without recording a successful upgrade', async function () { | ||
| await persist('5.0.0'); | ||
| const first = newAgent(); | ||
| first.clean.rejects(new Error('cleanup failed')); | ||
| await first.run(); | ||
| assert.equal(await persistedVersion(), '5.0.0'); | ||
| const second = newAgent(); | ||
| await second.run(); | ||
| sandbox.assert.calledOnce(second.clean); | ||
| assert.equal(await persistedVersion(), packageInfo.version); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.