Skip to content

Commit d9e42e5

Browse files
committed
Add support for caching uv
See https://github.com/astral-sh/uv
1 parent 19dfb7b commit d9e42e5

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

__tests__/cache-restore.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('restore-cache', () => {
1515
'2d0ff7f46b0e120e3d3294db65768b474934242637b9899b873e6283dfd16d7c';
1616
const poetryLockHash =
1717
'f24ea1ad73968e6c8d80c16a093ade72d9332c433aeef979a0dd943e6a99b2ab';
18+
const uvLockHash = 'efe9f18aef431b3f1dbe13bee790b00095e74fb19aa5ced5ace96d063f03258d';
1819
const poetryConfigOutput = `
1920
cache-dir = "/Users/patrick/Library/Caches/pypoetry"
2021
experimental.new-installer = false
@@ -153,6 +154,13 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
153154
path.join(__dirname, 'data', 'inner', '.venv'),
154155
path.join(__dirname, 'data', '.venv')
155156
]
157+
],
158+
[
159+
'uv',
160+
'3.12.0',
161+
'__tests__/data/**/pyproject.toml',
162+
uvLockHash,
163+
undefined,
156164
]
157165
])(
158166
'restored dependencies for %s by primaryKey',
@@ -188,6 +196,10 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
188196
result => result.value
189197
);
190198

199+
if(!restoredKeys.length) {
200+
throw new Error("No restored keys found, this probably means there's something wrong with the test");
201+
}
202+
191203
restoredKeys.forEach(restoredKey => {
192204
if (restoredKey) {
193205
if (process.platform === 'linux' && packageManager === 'pip') {

__tests__/cache-save.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('run', () => {
1313
'2d0ff7f46b0e120e3d3294db65768b474934242637b9899b873e6283dfd16d7c';
1414
const poetryLockHash =
1515
'571bf984f8d210e6a97f854e479fdd4a2b5af67b5fdac109ec337a0ea16e7836';
16+
const uvLockHash = 'TODO'; // TODO: what should be the correct value?
1617

1718
// core spy
1819
let infoSpy: jest.SpyInstance;
@@ -202,6 +203,34 @@ describe('run', () => {
202203
expect(setFailedSpy).not.toHaveBeenCalled();
203204
});
204205

206+
it('saves cache from uv', async () => {
207+
inputs['cache'] = 'uv';
208+
inputs['python-version'] = '3.12.0';
209+
getStateSpy.mockImplementation((name: string) => {
210+
if (name === State.CACHE_MATCHED_KEY) {
211+
console.log(name);
212+
return uvLockHash;
213+
} else if (name === State.CACHE_PATHS) {
214+
return JSON.stringify([__dirname]);
215+
} else {
216+
return requirementsHash;
217+
}
218+
});
219+
220+
await run();
221+
222+
expect(getInputSpy).toHaveBeenCalled();
223+
expect(getStateSpy).toHaveBeenCalledTimes(3);
224+
expect(infoSpy).not.toHaveBeenCalledWith(
225+
`Cache hit occurred on the primary key ${uvLockHash}, not saving cache.`
226+
);
227+
expect(saveCacheSpy).toHaveBeenCalled();
228+
expect(infoSpy).toHaveBeenLastCalledWith(
229+
`Cache saved with the key: ${requirementsHash}`
230+
);
231+
expect(setFailedSpy).not.toHaveBeenCalled();
232+
});
233+
205234
it('saves with -1 cacheId , should not fail workflow', async () => {
206235
inputs['cache'] = 'poetry';
207236
inputs['python-version'] = '3.10.0';

__tests__/data/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ pyinstaller = "5.13.1"
1414

1515
[build-system]
1616
requires = ["poetry-core>=1.0.0"]
17-
build-backend = "poetry.core.masonry.api"
17+
build-backend = "poetry.core.masonry.api"

dist/setup/index.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90291,11 +90291,13 @@ exports.getCacheDistributor = exports.PackageManagers = void 0;
9029190291
const pip_cache_1 = __importDefault(__nccwpck_require__(5546));
9029290292
const pipenv_cache_1 = __importDefault(__nccwpck_require__(238));
9029390293
const poetry_cache_1 = __importDefault(__nccwpck_require__(1993));
90294+
const uv_cache_1 = __importDefault(__nccwpck_require__(8795));
9029490295
var PackageManagers;
9029590296
(function (PackageManagers) {
9029690297
PackageManagers["Pip"] = "pip";
9029790298
PackageManagers["Pipenv"] = "pipenv";
9029890299
PackageManagers["Poetry"] = "poetry";
90300+
PackageManagers["Uv"] = "uv";
9029990301
})(PackageManagers || (exports.PackageManagers = PackageManagers = {}));
9030090302
function getCacheDistributor(packageManager, pythonVersion, cacheDependencyPath) {
9030190303
switch (packageManager) {
@@ -90305,6 +90307,8 @@ function getCacheDistributor(packageManager, pythonVersion, cacheDependencyPath)
9030590307
return new pipenv_cache_1.default(pythonVersion, cacheDependencyPath);
9030690308
case PackageManagers.Poetry:
9030790309
return new poetry_cache_1.default(pythonVersion, cacheDependencyPath);
90310+
case PackageManagers.Uv:
90311+
return new uv_cache_1.default(pythonVersion, cacheDependencyPath);
9030890312
default:
9030990313
throw new Error(`Caching for '${packageManager}' is not supported`);
9031090314
}
@@ -90679,6 +90683,88 @@ class PoetryCache extends cache_distributor_1.default {
9067990683
exports["default"] = PoetryCache;
9068090684

9068190685

90686+
/***/ }),
90687+
90688+
/***/ 8795:
90689+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
90690+
90691+
"use strict";
90692+
90693+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
90694+
if (k2 === undefined) k2 = k;
90695+
var desc = Object.getOwnPropertyDescriptor(m, k);
90696+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
90697+
desc = { enumerable: true, get: function() { return m[k]; } };
90698+
}
90699+
Object.defineProperty(o, k2, desc);
90700+
}) : (function(o, m, k, k2) {
90701+
if (k2 === undefined) k2 = k;
90702+
o[k2] = m[k];
90703+
}));
90704+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
90705+
Object.defineProperty(o, "default", { enumerable: true, value: v });
90706+
}) : function(o, v) {
90707+
o["default"] = v;
90708+
});
90709+
var __importStar = (this && this.__importStar) || function (mod) {
90710+
if (mod && mod.__esModule) return mod;
90711+
var result = {};
90712+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
90713+
__setModuleDefault(result, mod);
90714+
return result;
90715+
};
90716+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
90717+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
90718+
return new (P || (P = Promise))(function (resolve, reject) {
90719+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
90720+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
90721+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
90722+
step((generator = generator.apply(thisArg, _arguments || [])).next());
90723+
});
90724+
};
90725+
var __importDefault = (this && this.__importDefault) || function (mod) {
90726+
return (mod && mod.__esModule) ? mod : { "default": mod };
90727+
};
90728+
Object.defineProperty(exports, "__esModule", ({ value: true }));
90729+
const glob = __importStar(__nccwpck_require__(8090));
90730+
const os = __importStar(__nccwpck_require__(2037));
90731+
const path = __importStar(__nccwpck_require__(1017));
90732+
const cache_distributor_1 = __importDefault(__nccwpck_require__(8953));
90733+
class UvCache extends cache_distributor_1.default {
90734+
constructor(pythonVersion, patterns = '**/requirements.txt') {
90735+
super('uv', patterns);
90736+
this.pythonVersion = pythonVersion;
90737+
this.patterns = patterns;
90738+
}
90739+
getCacheGlobalDirectories() {
90740+
return __awaiter(this, void 0, void 0, function* () {
90741+
var _a;
90742+
if (process.platform === 'win32') {
90743+
// `LOCALAPPDATA` should always be defined,
90744+
// but we can't just join `undefined`
90745+
// into the path in case it's not.
90746+
return [
90747+
path.join((_a = process.env['LOCALAPPDATA']) !== null && _a !== void 0 ? _a : os.homedir(), 'uv', 'cache')
90748+
];
90749+
}
90750+
return [path.join(os.homedir(), '.cache/uv')];
90751+
});
90752+
}
90753+
computeKeys() {
90754+
return __awaiter(this, void 0, void 0, function* () {
90755+
const hash = yield glob.hashFiles(this.patterns);
90756+
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${process.arch}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
90757+
const restoreKey = undefined;
90758+
return {
90759+
primaryKey,
90760+
restoreKey
90761+
};
90762+
});
90763+
}
90764+
}
90765+
exports["default"] = UvCache;
90766+
90767+
9068290768
/***/ }),
9068390769

9068490770
/***/ 8040:

src/cache-distributions/cache-factory.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import PipCache from './pip-cache';
22
import PipenvCache from './pipenv-cache';
33
import PoetryCache from './poetry-cache';
4+
import UvCache from './uv-cache';
45

56
export enum PackageManagers {
67
Pip = 'pip',
78
Pipenv = 'pipenv',
8-
Poetry = 'poetry'
9+
Poetry = 'poetry',
10+
Uv = 'uv'
911
}
1012

1113
export function getCacheDistributor(
@@ -20,6 +22,8 @@ export function getCacheDistributor(
2022
return new PipenvCache(pythonVersion, cacheDependencyPath);
2123
case PackageManagers.Poetry:
2224
return new PoetryCache(pythonVersion, cacheDependencyPath);
25+
case PackageManagers.Uv:
26+
return new UvCache(pythonVersion, cacheDependencyPath);
2327
default:
2428
throw new Error(`Caching for '${packageManager}' is not supported`);
2529
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as glob from '@actions/glob';
2+
import * as os from 'os';
3+
import * as path from 'path';
4+
5+
import CacheDistributor from './cache-distributor';
6+
7+
export default class UvCache extends CacheDistributor {
8+
constructor(
9+
private pythonVersion: string,
10+
protected patterns: string = '**/requirements.txt'
11+
) {
12+
super('uv', patterns);
13+
}
14+
15+
protected async getCacheGlobalDirectories() {
16+
if (process.platform === 'win32') {
17+
// `LOCALAPPDATA` should always be defined,
18+
// but we can't just join `undefined`
19+
// into the path in case it's not.
20+
return [
21+
path.join(process.env['LOCALAPPDATA'] ?? os.homedir(), 'uv', 'cache')
22+
];
23+
}
24+
return [path.join(os.homedir(), '.cache/uv')];
25+
}
26+
27+
protected async computeKeys() {
28+
const hash = await glob.hashFiles(this.patterns);
29+
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${process.arch}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
30+
const restoreKey = undefined;
31+
return {
32+
primaryKey,
33+
restoreKey
34+
};
35+
}
36+
}

0 commit comments

Comments
 (0)