-
Notifications
You must be signed in to change notification settings - Fork 13
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
[ENG-9500] Implement save-cache and restore-cache functions #289
Closed
khamilowicz
wants to merge
4
commits into
main
from
piotrekszeremeta/eng-9500-caching-for-custom-builds
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01fba1e
[ENG-9500] Add save and restore cache functions
khamilowicz 712ac20
[ENG-9500] Allow calling functions with arguments in template files
khamilowicz 7ff63d7
[ENG-9500] Don't mix cacheManager with dynamic cache manager
khamilowicz fdb4f77
Apply review suggestions
khamilowicz 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
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
118 changes: 118 additions & 0 deletions
118
packages/build-tools/src/steps/functions/__tests__/cache.test.ts
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,118 @@ | ||
import fs from 'fs/promises'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
|
||
import { Job, Metadata } from '@expo/eas-build-job'; | ||
import { | ||
BuildRuntimePlatform, | ||
BuildStepGlobalContext, | ||
Cache, | ||
DynamicCacheManager, | ||
ExternalBuildContextProvider, | ||
} from '@expo/steps'; | ||
import { anything, capture, instance, mock, reset, verify, when } from 'ts-mockito'; | ||
|
||
import { createLogger } from '../../../__mocks__/@expo/logger'; | ||
import { createTestIosJob } from '../../../__tests__/utils/job'; | ||
import { createMockLogger } from '../../../__tests__/utils/logger'; | ||
import { BuildContext } from '../../../context'; | ||
import { CustomBuildContext } from '../../../customBuildContext'; | ||
import { createRestoreCacheBuildFunction, createSaveCacheBuildFunction } from '../cache'; | ||
|
||
const dynamicCacheManagerMock = mock<DynamicCacheManager>(); | ||
const dynamicCacheManager = instance(dynamicCacheManagerMock); | ||
|
||
const buildCtx = new BuildContext(createTestIosJob({}), { | ||
env: {}, | ||
logBuffer: { getLogs: () => [], getPhaseLogs: () => [] }, | ||
logger: createMockLogger(), | ||
uploadArtifact: jest.fn(), | ||
workingdir: '', | ||
dynamicCacheManager, | ||
}); | ||
const customContext = new CustomBuildContext(buildCtx); | ||
|
||
const cacheSaveBuildFunction = createSaveCacheBuildFunction(customContext); | ||
const cacheRestoreBuildFunction = createRestoreCacheBuildFunction(customContext); | ||
|
||
const providerMock = mock<ExternalBuildContextProvider>(); | ||
|
||
const initialCache: Cache = { disabled: false, clear: false, paths: [] }; | ||
|
||
const provider = instance(providerMock); | ||
|
||
let ctx: BuildStepGlobalContext; | ||
|
||
describe('cache functions', () => { | ||
let key: string; | ||
let paths: string[]; | ||
beforeEach(async () => { | ||
key = '${ hashFiles("./src/*") }-value'; | ||
paths = ['path1', 'path2']; | ||
reset(dynamicCacheManagerMock); | ||
reset(providerMock); | ||
|
||
const projectSourceDirectory = await fs.mkdtemp(path.join(os.tmpdir(), 'project-')); | ||
when(providerMock.logger).thenReturn(createLogger()); | ||
when(providerMock.runtimePlatform).thenReturn(BuildRuntimePlatform.LINUX); | ||
when(providerMock.staticContext()).thenReturn({ | ||
metadata: {} as Metadata, | ||
env: {}, | ||
job: { cache: initialCache } as Job, | ||
}); | ||
when(providerMock.projectSourceDirectory).thenReturn(projectSourceDirectory); | ||
when(providerMock.defaultWorkingDirectory).thenReturn(projectSourceDirectory); | ||
when(providerMock.projectTargetDirectory).thenReturn(projectSourceDirectory); | ||
|
||
ctx = new BuildStepGlobalContext(provider, false); | ||
|
||
await fs.mkdir(path.join(projectSourceDirectory, 'src')); | ||
await fs.writeFile(path.join(projectSourceDirectory, 'src', 'path1'), 'placeholder'); | ||
await fs.writeFile(path.join(projectSourceDirectory, 'src', 'path2'), 'placeholder'); | ||
}); | ||
|
||
describe('cacheRestoreBuildFunction', () => { | ||
test('has correct identifiers', () => { | ||
expect(cacheRestoreBuildFunction.id).toBe('restore-cache'); | ||
expect(cacheRestoreBuildFunction.namespace).toBe('eas'); | ||
expect(cacheRestoreBuildFunction.name).toBe('Restore Cache'); | ||
}); | ||
|
||
test('restores cache if it exists', async () => { | ||
const buildStep = cacheRestoreBuildFunction.createBuildStepFromFunctionCall(ctx, { | ||
callInputs: { key, paths }, | ||
}); | ||
|
||
when(providerMock.defaultWorkingDirectory).thenReturn('/tmp'); | ||
|
||
await buildStep.executeAsync(); | ||
|
||
verify(dynamicCacheManagerMock.restoreCache(anything(), anything())).once(); | ||
|
||
const [, cache] = capture(dynamicCacheManagerMock.restoreCache).first(); | ||
expect(cache.key).toMatch(/^\w+-value/); | ||
expect(cache.paths).toStrictEqual(paths); | ||
}); | ||
}); | ||
|
||
describe('cacheSaveBuildFunction', () => { | ||
test('has correct identifiers', () => { | ||
expect(cacheSaveBuildFunction.id).toBe('save-cache'); | ||
expect(cacheSaveBuildFunction.namespace).toBe('eas'); | ||
expect(cacheSaveBuildFunction.name).toBe('Save Cache'); | ||
}); | ||
|
||
test('saves cache if it does not exist', async () => { | ||
const buildStep = cacheSaveBuildFunction.createBuildStepFromFunctionCall(ctx, { | ||
callInputs: { key, paths }, | ||
}); | ||
|
||
await buildStep.executeAsync(); | ||
|
||
verify(dynamicCacheManagerMock.saveCache(anything(), anything())).once(); | ||
|
||
const [, cache] = capture(dynamicCacheManagerMock.saveCache).first(); | ||
expect(cache?.key).toMatch(/^\w+-value/); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
We could consider rolling
cacheManager
intoruntimeApi
for simplicity. Like, how many functions may we put intoruntimeApi
? I don't think the list will be too long.