Skip to content

Commit d583854

Browse files
committed
Rename functions & add e2e tests
1 parent 62a8f25 commit d583854

File tree

6 files changed

+189
-39
lines changed

6 files changed

+189
-39
lines changed

.github/workflows/e2e-cache.yml

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ jobs:
146146
- uses: actions/checkout@v3
147147

148148
- name: prepare sub-projects
149-
run: __tests__/prepare-subprojects.sh
149+
run: __tests__/prepare-yarn-subprojects.sh yarn1
150150

151151
# expect
152152
# - no errors
@@ -161,3 +161,85 @@ jobs:
161161
cache-dependency-path: |
162162
**/*.lock
163163
yarn.lock
164+
165+
yarn-subprojects-berry-local:
166+
name: Test yarn subprojects all locally managed
167+
strategy:
168+
matrix:
169+
node-version: [12, 14, 16]
170+
runs-on: ubuntu-latest
171+
172+
steps:
173+
- uses: actions/checkout@v3
174+
175+
- name: prepare sub-projects
176+
run: __tests__/prepare-yarn-subprojects.sh
177+
178+
# expect
179+
# - no errors
180+
# - log
181+
# ##[info]All dependencies are managed locally by yarn3, the previous cache can be used
182+
# ##[debug]["node-cache-Linux-yarn-401024703386272f1a950c9f014cbb1bb79a7a5b6e1fb00e8b90d06734af41ee","node-cache-Linux-yarn"]
183+
- name: Setup Node
184+
uses: ./
185+
with:
186+
node-version: ${{ matrix.node-version }}
187+
cache: 'yarn'
188+
cache-dependency-path: |
189+
sub2/*.lock
190+
sub3/*.lock
191+
192+
yarn-subprojects-berry-global:
193+
name: Test yarn subprojects some locally managed
194+
strategy:
195+
matrix:
196+
node-version: [12, 14, 16]
197+
runs-on: ubuntu-latest
198+
199+
steps:
200+
- uses: actions/checkout@v3
201+
202+
- name: prepare sub-projects
203+
run: __tests__/prepare-yarn-subprojects.sh global
204+
205+
# expect
206+
# - no errors
207+
# - log must
208+
# ##[debug]"/home/runner/work/setup-node-test/setup-node-test/sub2" dependencies are managed by yarn 3 locally
209+
# ##[debug]"/home/runner/work/setup-node-test/setup-node-test/sub3" dependencies are not managed by yarn 3 locally
210+
- name: Setup Node
211+
uses: ./
212+
with:
213+
node-version: ${{ matrix.node-version }}
214+
cache: 'yarn'
215+
cache-dependency-path: |
216+
sub2/*.lock
217+
sub3/*.lock
218+
219+
yarn-subprojects-berry-git:
220+
name: Test yarn subprojects managed by git
221+
strategy:
222+
matrix:
223+
node-version: [12, 14, 16]
224+
runs-on: ubuntu-latest
225+
226+
steps:
227+
- uses: actions/checkout@v3
228+
229+
- name: prepare sub-projects
230+
run: /bin/bash __tests__/prepare-yarn-subprojects.sh keepcache
231+
232+
# expect
233+
# - no errors
234+
# - log
235+
# [debug]"/home/runner/work/setup-node-test/setup-node-test/sub2" has .yarn/cache - dependencies are kept in the repository
236+
# [debug]"/home/runner/work/setup-node-test/setup-node-test/sub3" has .yarn/cache - dependencies are kept in the repository
237+
# [debug]["node-cache-Linux-yarn-401024703386272f1a950c9f014cbb1bb79a7a5b6e1fb00e8b90d06734af41ee"]
238+
- name: Setup Node
239+
uses: ./
240+
with:
241+
node-version: ${{ matrix.node-version }}
242+
cache: 'yarn'
243+
cache-dependency-path: |
244+
sub2/*.lock
245+
sub3/*.lock

__tests__/prepare-subprojects.sh renamed to __tests__/prepare-yarn-subprojects.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,20 @@ cat <<EOT >package.json
3232
EOT
3333
yarn set version 3.5.1
3434
yarn install
35+
if [ x$1 = 'xglobal' ];then
36+
echo enableGlobalCache
37+
echo 'enableGlobalCache: true' >> .yarnrc.yml
38+
fi
3539

36-
echo "create yarn1 project in the root"
3740
cd ..
38-
cat <<EOT >package.json
41+
if [ x$1 != 'xkeepcache' -a x$2 != 'xkeepcache' ]; then
42+
rm -rf sub2/.yarn/cache
43+
rm -rf sub3/.yarn/cache
44+
fi
45+
46+
if [ x$1 = 'xyarn1' ];then
47+
echo "create yarn1 project in the root"
48+
cat <<EOT >package.json
3949
{
4050
"name": "subproject",
4151
"dependencies": {
@@ -44,5 +54,6 @@ cat <<EOT >package.json
4454
}
4555
}
4656
EOT
47-
yarn set version 1.22.19
48-
yarn install
57+
yarn set version 1.22.19
58+
yarn install
59+
fi

dist/cache-save/index.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60434,7 +60434,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6043460434
return (mod && mod.__esModule) ? mod : { "default": mod };
6043560435
};
6043660436
Object.defineProperty(exports, "__esModule", ({ value: true }));
60437-
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarn3ManagedCache = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
60437+
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarnBerryManagedDependencies = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
6043860438
const core = __importStar(__nccwpck_require__(2186));
6043960439
const exec = __importStar(__nccwpck_require__(1514));
6044060440
const cache = __importStar(__nccwpck_require__(7799));
@@ -60548,7 +60548,7 @@ const getProjectDirectoriesFromCacheDependencyPath = (cacheDependencyPath) => __
6054860548
const getCacheDirectoriesFromCacheDependencyPath = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
6054960549
const projectDirectories = yield getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath);
6055060550
const cacheFoldersPaths = yield Promise.all(projectDirectories.map((projectDirectory) => __awaiter(void 0, void 0, void 0, function* () {
60551-
const cacheFolderPath = packageManagerInfo.getCacheFolderPath(projectDirectory);
60551+
const cacheFolderPath = yield packageManagerInfo.getCacheFolderPath(projectDirectory);
6055260552
core.debug(`${packageManagerInfo.name}'s cache folder "${cacheFolderPath}" configured for the directory "${projectDirectory}"`);
6055360553
return cacheFolderPath;
6055460554
})));
@@ -60592,16 +60592,28 @@ exports.getCacheDirectories = getCacheDirectories;
6059260592
* - if local cache is not explicitly enabled (not yarn3), return false
6059360593
* - return true otherwise
6059460594
*/
60595-
const isCacheManagedByYarn3 = (directory) => __awaiter(void 0, void 0, void 0, function* () {
60595+
const projectHasYarnBerryManagedDependencies = (directory) => __awaiter(void 0, void 0, void 0, function* () {
6059660596
const workDir = directory || process.env.GITHUB_WORKSPACE || '.';
60597+
core.debug(`check if "${workDir}" has locally managed yarn3 dependencies`);
6059760598
// if .yarn/cache directory exists the cache is managed by version control system
6059860599
const yarnCacheFile = path_1.default.join(workDir, '.yarn', 'cache');
60599-
if (fs_1.default.existsSync(yarnCacheFile) && fs_1.default.lstatSync(yarnCacheFile).isDirectory())
60600+
if (fs_1.default.existsSync(yarnCacheFile) &&
60601+
fs_1.default.lstatSync(yarnCacheFile).isDirectory()) {
60602+
core.debug(`"${workDir}" has .yarn/cache - dependencies are kept in the repository`);
6060060603
return Promise.resolve(false);
60601-
// NOTE: yarn1 returns 'undefined' with rc = 0
60604+
}
60605+
// NOTE: yarn1 returns 'undefined' with return code = 0
6060260606
const enableGlobalCache = yield exports.getCommandOutput('yarn config get enableGlobalCache', workDir);
6060360607
// only local cache is not managed by yarn
60604-
return enableGlobalCache === 'false';
60608+
const managed = enableGlobalCache.includes('false');
60609+
if (managed) {
60610+
core.debug(`"${workDir}" dependencies are managed by yarn 3 locally`);
60611+
return true;
60612+
}
60613+
else {
60614+
core.debug(`"${workDir}" dependencies are not managed by yarn 3 locally`);
60615+
return false;
60616+
}
6060560617
});
6060660618
/**
6060760619
* A function to report the repo contains Yarn managed projects
@@ -60610,16 +60622,16 @@ const isCacheManagedByYarn3 = (directory) => __awaiter(void 0, void 0, void 0, f
6061060622
* expected to be the result of `core.getInput('cache-dependency-path')`
6061160623
* @return - true if all project directories configured to be Yarn managed
6061260624
*/
60613-
const repoHasYarn3ManagedCache = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
60625+
const repoHasYarnBerryManagedDependencies = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
6061460626
if (packageManagerInfo.name !== 'yarn')
6061560627
return false;
6061660628
const yarnDirs = cacheDependencyPath
6061760629
? yield getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath)
6061860630
: [''];
60619-
const isManagedList = yield Promise.all(yarnDirs.map(isCacheManagedByYarn3));
60631+
const isManagedList = yield Promise.all(yarnDirs.map(projectHasYarnBerryManagedDependencies));
6062060632
return isManagedList.every(Boolean);
6062160633
});
60622-
exports.repoHasYarn3ManagedCache = repoHasYarn3ManagedCache;
60634+
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
6062360635
function isGhes() {
6062460636
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
6062560637
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';

dist/setup/index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71157,9 +71157,15 @@ const restoreCache = (packageManager, cacheDependencyPath) => __awaiter(void 0,
7115771157
const primaryKey = `${keyPrefix}-${fileHash}`;
7115871158
core.debug(`primary key is ${primaryKey}`);
7115971159
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
71160-
const cacheKey = (yield cache_utils_1.repoHasYarn3ManagedCache(packageManagerInfo, cacheDependencyPath))
71161-
? yield cache.restoreCache(cachePaths, primaryKey, [keyPrefix])
71162-
: yield cache.restoreCache(cachePaths, primaryKey);
71160+
const isManagedByYarn3 = yield cache_utils_1.repoHasYarnBerryManagedDependencies(packageManagerInfo, cacheDependencyPath);
71161+
let cacheKey;
71162+
if (isManagedByYarn3) {
71163+
core.info('All dependencies are managed locally by yarn3, the previous cache can be used');
71164+
cacheKey = yield cache.restoreCache(cachePaths, primaryKey, [keyPrefix]);
71165+
}
71166+
else {
71167+
cacheKey = yield cache.restoreCache(cachePaths, primaryKey);
71168+
}
7116371169
core.setOutput('cache-hit', Boolean(cacheKey));
7116471170
if (!cacheKey) {
7116571171
core.info(`${packageManager} cache is not found`);
@@ -71220,7 +71226,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7122071226
return (mod && mod.__esModule) ? mod : { "default": mod };
7122171227
};
7122271228
Object.defineProperty(exports, "__esModule", ({ value: true }));
71223-
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarn3ManagedCache = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
71229+
exports.isCacheFeatureAvailable = exports.isGhes = exports.repoHasYarnBerryManagedDependencies = exports.getCacheDirectories = exports.resetProjectDirectoriesMemoized = exports.getPackageManagerInfo = exports.getCommandOutputNotEmpty = exports.getCommandOutput = exports.supportedPackageManagers = void 0;
7122471230
const core = __importStar(__nccwpck_require__(2186));
7122571231
const exec = __importStar(__nccwpck_require__(1514));
7122671232
const cache = __importStar(__nccwpck_require__(7799));
@@ -71334,7 +71340,7 @@ const getProjectDirectoriesFromCacheDependencyPath = (cacheDependencyPath) => __
7133471340
const getCacheDirectoriesFromCacheDependencyPath = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
7133571341
const projectDirectories = yield getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath);
7133671342
const cacheFoldersPaths = yield Promise.all(projectDirectories.map((projectDirectory) => __awaiter(void 0, void 0, void 0, function* () {
71337-
const cacheFolderPath = packageManagerInfo.getCacheFolderPath(projectDirectory);
71343+
const cacheFolderPath = yield packageManagerInfo.getCacheFolderPath(projectDirectory);
7133871344
core.debug(`${packageManagerInfo.name}'s cache folder "${cacheFolderPath}" configured for the directory "${projectDirectory}"`);
7133971345
return cacheFolderPath;
7134071346
})));
@@ -71378,16 +71384,28 @@ exports.getCacheDirectories = getCacheDirectories;
7137871384
* - if local cache is not explicitly enabled (not yarn3), return false
7137971385
* - return true otherwise
7138071386
*/
71381-
const isCacheManagedByYarn3 = (directory) => __awaiter(void 0, void 0, void 0, function* () {
71387+
const projectHasYarnBerryManagedDependencies = (directory) => __awaiter(void 0, void 0, void 0, function* () {
7138271388
const workDir = directory || process.env.GITHUB_WORKSPACE || '.';
71389+
core.debug(`check if "${workDir}" has locally managed yarn3 dependencies`);
7138371390
// if .yarn/cache directory exists the cache is managed by version control system
7138471391
const yarnCacheFile = path_1.default.join(workDir, '.yarn', 'cache');
71385-
if (fs_1.default.existsSync(yarnCacheFile) && fs_1.default.lstatSync(yarnCacheFile).isDirectory())
71392+
if (fs_1.default.existsSync(yarnCacheFile) &&
71393+
fs_1.default.lstatSync(yarnCacheFile).isDirectory()) {
71394+
core.debug(`"${workDir}" has .yarn/cache - dependencies are kept in the repository`);
7138671395
return Promise.resolve(false);
71387-
// NOTE: yarn1 returns 'undefined' with rc = 0
71396+
}
71397+
// NOTE: yarn1 returns 'undefined' with return code = 0
7138871398
const enableGlobalCache = yield exports.getCommandOutput('yarn config get enableGlobalCache', workDir);
7138971399
// only local cache is not managed by yarn
71390-
return enableGlobalCache === 'false';
71400+
const managed = enableGlobalCache.includes('false');
71401+
if (managed) {
71402+
core.debug(`"${workDir}" dependencies are managed by yarn 3 locally`);
71403+
return true;
71404+
}
71405+
else {
71406+
core.debug(`"${workDir}" dependencies are not managed by yarn 3 locally`);
71407+
return false;
71408+
}
7139171409
});
7139271410
/**
7139371411
* A function to report the repo contains Yarn managed projects
@@ -71396,16 +71414,16 @@ const isCacheManagedByYarn3 = (directory) => __awaiter(void 0, void 0, void 0, f
7139671414
* expected to be the result of `core.getInput('cache-dependency-path')`
7139771415
* @return - true if all project directories configured to be Yarn managed
7139871416
*/
71399-
const repoHasYarn3ManagedCache = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
71417+
const repoHasYarnBerryManagedDependencies = (packageManagerInfo, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
7140071418
if (packageManagerInfo.name !== 'yarn')
7140171419
return false;
7140271420
const yarnDirs = cacheDependencyPath
7140371421
? yield getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath)
7140471422
: [''];
71405-
const isManagedList = yield Promise.all(yarnDirs.map(isCacheManagedByYarn3));
71423+
const isManagedList = yield Promise.all(yarnDirs.map(projectHasYarnBerryManagedDependencies));
7140671424
return isManagedList.every(Boolean);
7140771425
});
71408-
exports.repoHasYarn3ManagedCache = repoHasYarn3ManagedCache;
71426+
exports.repoHasYarnBerryManagedDependencies = repoHasYarnBerryManagedDependencies;
7140971427
function isGhes() {
7141071428
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
7141171429
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';

src/cache-restore.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {State} from './constants';
88
import {
99
getCacheDirectories,
1010
getPackageManagerInfo,
11-
repoHasYarn3ManagedCache,
11+
repoHasYarnBerryManagedDependencies,
1212
PackageManagerInfo
1313
} from './cache-utils';
1414

@@ -44,12 +44,19 @@ export const restoreCache = async (
4444

4545
core.saveState(State.CachePrimaryKey, primaryKey);
4646

47-
const cacheKey = (await repoHasYarn3ManagedCache(
47+
const isManagedByYarn3 = await repoHasYarnBerryManagedDependencies(
4848
packageManagerInfo,
4949
cacheDependencyPath
50-
))
51-
? await cache.restoreCache(cachePaths, primaryKey, [keyPrefix])
52-
: await cache.restoreCache(cachePaths, primaryKey);
50+
);
51+
let cacheKey: string | undefined;
52+
if (isManagedByYarn3) {
53+
core.info(
54+
'All dependencies are managed locally by yarn3, the previous cache can be used'
55+
);
56+
cacheKey = await cache.restoreCache(cachePaths, primaryKey, [keyPrefix]);
57+
} else {
58+
cacheKey = await cache.restoreCache(cachePaths, primaryKey);
59+
}
5360

5461
core.setOutput('cache-hit', Boolean(cacheKey));
5562

src/cache-utils.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ const getCacheDirectoriesFromCacheDependencyPath = async (
171171
);
172172
const cacheFoldersPaths = await Promise.all(
173173
projectDirectories.map(async projectDirectory => {
174-
const cacheFolderPath =
175-
packageManagerInfo.getCacheFolderPath(projectDirectory);
174+
const cacheFolderPath = await packageManagerInfo.getCacheFolderPath(
175+
projectDirectory
176+
);
176177
core.debug(
177178
`${packageManagerInfo.name}'s cache folder "${cacheFolderPath}" configured for the directory "${projectDirectory}"`
178179
);
@@ -231,21 +232,38 @@ export const getCacheDirectories = async (
231232
* - if local cache is not explicitly enabled (not yarn3), return false
232233
* - return true otherwise
233234
*/
234-
const isCacheManagedByYarn3 = async (directory: string): Promise<boolean> => {
235+
const projectHasYarnBerryManagedDependencies = async (
236+
directory: string
237+
): Promise<boolean> => {
235238
const workDir = directory || process.env.GITHUB_WORKSPACE || '.';
239+
core.debug(`check if "${workDir}" has locally managed yarn3 dependencies`);
236240

237241
// if .yarn/cache directory exists the cache is managed by version control system
238242
const yarnCacheFile = path.join(workDir, '.yarn', 'cache');
239-
if (fs.existsSync(yarnCacheFile) && fs.lstatSync(yarnCacheFile).isDirectory())
243+
if (
244+
fs.existsSync(yarnCacheFile) &&
245+
fs.lstatSync(yarnCacheFile).isDirectory()
246+
) {
247+
core.debug(
248+
`"${workDir}" has .yarn/cache - dependencies are kept in the repository`
249+
);
240250
return Promise.resolve(false);
251+
}
241252

242-
// NOTE: yarn1 returns 'undefined' with rc = 0
253+
// NOTE: yarn1 returns 'undefined' with return code = 0
243254
const enableGlobalCache = await getCommandOutput(
244255
'yarn config get enableGlobalCache',
245256
workDir
246257
);
247258
// only local cache is not managed by yarn
248-
return enableGlobalCache === 'false';
259+
const managed = enableGlobalCache.includes('false');
260+
if (managed) {
261+
core.debug(`"${workDir}" dependencies are managed by yarn 3 locally`);
262+
return true;
263+
} else {
264+
core.debug(`"${workDir}" dependencies are not managed by yarn 3 locally`);
265+
return false;
266+
}
249267
};
250268

251269
/**
@@ -255,7 +273,7 @@ const isCacheManagedByYarn3 = async (directory: string): Promise<boolean> => {
255273
* expected to be the result of `core.getInput('cache-dependency-path')`
256274
* @return - true if all project directories configured to be Yarn managed
257275
*/
258-
export const repoHasYarn3ManagedCache = async (
276+
export const repoHasYarnBerryManagedDependencies = async (
259277
packageManagerInfo: PackageManagerInfo,
260278
cacheDependencyPath: string
261279
): Promise<boolean> => {
@@ -265,7 +283,9 @@ export const repoHasYarn3ManagedCache = async (
265283
? await getProjectDirectoriesFromCacheDependencyPath(cacheDependencyPath)
266284
: [''];
267285

268-
const isManagedList = await Promise.all(yarnDirs.map(isCacheManagedByYarn3));
286+
const isManagedList = await Promise.all(
287+
yarnDirs.map(projectHasYarnBerryManagedDependencies)
288+
);
269289

270290
return isManagedList.every(Boolean);
271291
};

0 commit comments

Comments
 (0)