Unverified Commit 354a2ae1 authored by Shubham Tiwari's avatar Shubham Tiwari Committed by GitHub
Browse files

Consuming 3.0 actions/cache (#834)

* Consuming 3.0 actions/cache

* formatting and error

* updated package version

* resolve package

* dist

* review comment

* dist

* dist
parent baed3516
Loading
Loading
Loading
Loading

File changed.

Preview suppressed by a .gitattributes entry or the file's encoding is unsupported.

−34
Changes for __tests__/restore.test.ts: 0 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -227,40 +227,6 @@ test("restore with no cache found", async () => {
    );
});

test("restore with server error should fail", async () => {
    const path = "node_modules";
    const key = "node-test";
    testUtils.setInputs({
        path: path,
        key
    });

    const logWarningMock = jest.spyOn(actionUtils, "logWarning");
    const failedMock = jest.spyOn(core, "setFailed");
    const stateMock = jest.spyOn(core, "saveState");
    const restoreCacheMock = jest
        .spyOn(cache, "restoreCache")
        .mockImplementationOnce(() => {
            throw new Error("HTTP Error Occurred");
        });
    const setCacheHitOutputMock = jest.spyOn(actionUtils, "setCacheHitOutput");

    await run();

    expect(restoreCacheMock).toHaveBeenCalledTimes(1);
    expect(restoreCacheMock).toHaveBeenCalledWith([path], key, []);

    expect(stateMock).toHaveBeenCalledWith("CACHE_KEY", key);

    expect(logWarningMock).toHaveBeenCalledTimes(1);
    expect(logWarningMock).toHaveBeenCalledWith("HTTP Error Occurred");

    expect(setCacheHitOutputMock).toHaveBeenCalledTimes(1);
    expect(setCacheHitOutputMock).toHaveBeenCalledWith(false);

    expect(failedMock).toHaveBeenCalledTimes(0);
});

test("restore with restore keys and no cache found", async () => {
    const path = "node_modules";
    const key = "node-test";
+2 −3
Changes for __tests__/save.test.ts: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -267,7 +267,6 @@ test("save with large cache outputs warning", async () => {
});

test("save with reserve cache failure outputs warning", async () => {
    const infoMock = jest.spyOn(core, "info");
    const logWarningMock = jest.spyOn(actionUtils, "logWarning");
    const failedMock = jest.spyOn(core, "setFailed");

@@ -306,10 +305,10 @@ test("save with reserve cache failure outputs warning", async () => {
        expect.anything()
    );

    expect(infoMock).toHaveBeenCalledWith(
    expect(logWarningMock).toHaveBeenCalledWith(
        `Unable to reserve cache with key ${primaryKey}, another job may be creating this cache.`
    );
    expect(logWarningMock).toHaveBeenCalledTimes(0);
    expect(logWarningMock).toHaveBeenCalledTimes(1);
    expect(failedMock).toHaveBeenCalledTimes(0);
});

+28 −16
Changes for dist/restore/index.js: 28 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -46850,6 +46850,8 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
            checkKey(key);
        }
        const compressionMethod = yield utils.getCompressionMethod();
        let archivePath = '';
        try {
            // path are needed to compute version
            const cacheEntry = yield cacheHttpClient.getCacheEntry(keys, paths, {
                compressionMethod
@@ -46858,9 +46860,8 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
                // Cache not found
                return undefined;
            }
        const archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
            archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
            core.debug(`Archive Path: ${archivePath}`);
        try {
            // Download the cache from the cache entry
            yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath, options);
            if (core.isDebug()) {
@@ -46870,6 +46871,17 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
            core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`);
            yield tar_1.extractTar(archivePath, compressionMethod);
            core.info('Cache restored successfully');
            return cacheEntry.cacheKey;
        }
        catch (error) {
            const typedError = error;
            if (typedError.name === ValidationError.name) {
                throw error;
            }
            else {
                // Supress all non-validation cache related errors because caching should be optional
                core.warning(`Failed to restore: ${error.message}`);
            }
        }
        finally {
            // Try to delete the archive to save space
@@ -46880,7 +46892,7 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
                core.debug(`Failed to delete archive: ${error}`);
            }
        }
        return cacheEntry.cacheKey;
        return undefined;
    });
}
exports.restoreCache = restoreCache;
@@ -46898,7 +46910,7 @@ function saveCache(paths, key, options) {
        checkPaths(paths);
        checkKey(key);
        const compressionMethod = yield utils.getCompressionMethod();
        let cacheId = null;
        let cacheId = -1;
        const cachePaths = yield utils.resolvePaths(paths);
        core.debug('Cache Paths:');
        core.debug(`${JSON.stringify(cachePaths)}`);
@@ -46937,6 +46949,18 @@ function saveCache(paths, key, options) {
            core.debug(`Saving Cache (ID: ${cacheId})`);
            yield cacheHttpClient.saveCache(cacheId, archivePath, options);
        }
        catch (error) {
            const typedError = error;
            if (typedError.name === ValidationError.name) {
                throw error;
            }
            else if (typedError.name === ReserveCacheError.name) {
                core.info(`Failed to save: ${typedError.message}`);
            }
            else {
                core.warning(`Failed to save: ${typedError.message}`);
            }
        }
        finally {
            // Try to delete the archive to save space
            try {
@@ -48996,7 +49020,6 @@ function run() {
            const cachePaths = utils.getInputAsArray(constants_1.Inputs.Path, {
                required: true
            });
            try {
            const cacheKey = yield cache.restoreCache(cachePaths, primaryKey, restoreKeys);
            if (!cacheKey) {
                core.info(`Cache not found for input keys: ${[
@@ -49011,17 +49034,6 @@ function run() {
            utils.setCacheHitOutput(isExactKeyMatch);
            core.info(`Cache restored from key: ${cacheKey}`);
        }
            catch (error) {
                const typedError = error;
                if (typedError.name === cache.ValidationError.name) {
                    throw error;
                }
                else {
                    utils.logWarning(typedError.message);
                    utils.setCacheHitOutput(false);
                }
            }
        }
        catch (error) {
            core.setFailed(error.message);
        }
+30 −18
Changes for dist/save/index.js: 30 added lines, 18 removed lines.
Original line number Diff line number Diff line
@@ -46792,24 +46792,12 @@ function run() {
            const cachePaths = utils.getInputAsArray(constants_1.Inputs.Path, {
                required: true
            });
            try {
                yield cache.saveCache(cachePaths, primaryKey, {
            const cacheId = yield cache.saveCache(cachePaths, primaryKey, {
                uploadChunkSize: utils.getInputAsInt(constants_1.Inputs.UploadChunkSize)
            });
            if (cacheId != -1) {
                core.info(`Cache saved with key: ${primaryKey}`);
            }
            catch (error) {
                const typedError = error;
                if (typedError.name === cache.ValidationError.name) {
                    throw error;
                }
                else if (typedError.name === cache.ReserveCacheError.name) {
                    core.info(typedError.message);
                }
                else {
                    utils.logWarning(typedError.message);
                }
            }
        }
        catch (error) {
            utils.logWarning(error.message);
@@ -46948,6 +46936,8 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
            checkKey(key);
        }
        const compressionMethod = yield utils.getCompressionMethod();
        let archivePath = '';
        try {
            // path are needed to compute version
            const cacheEntry = yield cacheHttpClient.getCacheEntry(keys, paths, {
                compressionMethod
@@ -46956,9 +46946,8 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
                // Cache not found
                return undefined;
            }
        const archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
            archivePath = path.join(yield utils.createTempDirectory(), utils.getCacheFileName(compressionMethod));
            core.debug(`Archive Path: ${archivePath}`);
        try {
            // Download the cache from the cache entry
            yield cacheHttpClient.downloadCache(cacheEntry.archiveLocation, archivePath, options);
            if (core.isDebug()) {
@@ -46968,6 +46957,17 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
            core.info(`Cache Size: ~${Math.round(archiveFileSize / (1024 * 1024))} MB (${archiveFileSize} B)`);
            yield tar_1.extractTar(archivePath, compressionMethod);
            core.info('Cache restored successfully');
            return cacheEntry.cacheKey;
        }
        catch (error) {
            const typedError = error;
            if (typedError.name === ValidationError.name) {
                throw error;
            }
            else {
                // Supress all non-validation cache related errors because caching should be optional
                core.warning(`Failed to restore: ${error.message}`);
            }
        }
        finally {
            // Try to delete the archive to save space
@@ -46978,7 +46978,7 @@ function restoreCache(paths, primaryKey, restoreKeys, options) {
                core.debug(`Failed to delete archive: ${error}`);
            }
        }
        return cacheEntry.cacheKey;
        return undefined;
    });
}
exports.restoreCache = restoreCache;
@@ -46996,7 +46996,7 @@ function saveCache(paths, key, options) {
        checkPaths(paths);
        checkKey(key);
        const compressionMethod = yield utils.getCompressionMethod();
        let cacheId = null;
        let cacheId = -1;
        const cachePaths = yield utils.resolvePaths(paths);
        core.debug('Cache Paths:');
        core.debug(`${JSON.stringify(cachePaths)}`);
@@ -47035,6 +47035,18 @@ function saveCache(paths, key, options) {
            core.debug(`Saving Cache (ID: ${cacheId})`);
            yield cacheHttpClient.saveCache(cacheId, archivePath, options);
        }
        catch (error) {
            const typedError = error;
            if (typedError.name === ValidationError.name) {
                throw error;
            }
            else if (typedError.name === ReserveCacheError.name) {
                core.info(`Failed to save: ${typedError.message}`);
            }
            else {
                core.warning(`Failed to save: ${typedError.message}`);
            }
        }
        finally {
            // Try to delete the archive to save space
            try {
Loading