Avoid NockMock warnings during standalone executable testing

Change-type: patch
This commit is contained in:
Paulo Castro 2021-09-29 16:33:56 +01:00
parent 6cfff72c59
commit be306e6a20
4 changed files with 14 additions and 19 deletions

View File

@ -97,7 +97,7 @@ async function init() {
async function oclifRun(command: string[], options: AppOptions) { async function oclifRun(command: string[], options: AppOptions) {
let deprecationPromise: Promise<void>; let deprecationPromise: Promise<void>;
// check and enforce the CLI's deprecation policy // check and enforce the CLI's deprecation policy
if (unsupportedFlag) { if (unsupportedFlag || process.env.BALENARC_UNSUPPORTED) {
deprecationPromise = Promise.resolve(); deprecationPromise = Promise.resolve();
} else { } else {
const { DeprecationChecker } = await import('./deprecation'); const { DeprecationChecker } = await import('./deprecation');

View File

@ -38,7 +38,6 @@ export interface ReleaseTimestampsByVersion {
* https://jel.ly.fish/ed8d2395-9323-418c-bb67-d11d32a17d00 * https://jel.ly.fish/ed8d2395-9323-418c-bb67-d11d32a17d00
*/ */
export class DeprecationChecker { export class DeprecationChecker {
protected static disabled = false; // for the benefit of testing
readonly majorVersionFetchIntervalDays = 7; readonly majorVersionFetchIntervalDays = 7;
readonly expiryDays = 365; readonly expiryDays = 365;
readonly deprecationDays = Math.ceil(this.expiryDays / 2); readonly deprecationDays = Math.ceil(this.expiryDays / 2);
@ -131,7 +130,7 @@ or release date not available`);
* `majorVersionFetchIntervalDays`. * `majorVersionFetchIntervalDays`.
*/ */
public async checkForNewReleasesIfNeeded() { public async checkForNewReleasesIfNeeded() {
if (DeprecationChecker.disabled) { if (process.env.BALENARC_UNSUPPORTED) {
return; // for the benefit of code testing return; // for the benefit of code testing
} }
await this.init(); await this.init();
@ -182,7 +181,7 @@ or release date not available`);
* in which case warn about it and conditionally throw an error. * in which case warn about it and conditionally throw an error.
*/ */
public async warnAndAbortIfDeprecated() { public async warnAndAbortIfDeprecated() {
if (DeprecationChecker.disabled) { if (process.env.BALENARC_UNSUPPORTED) {
return; // for the benefit of code testing return; // for the benefit of code testing
} }
await this.init(); await this.init();
@ -228,14 +227,4 @@ continue using this version of the CLI. However, note that the balenaCloud
or openBalena backends may be updated in a way that is no longer compatible or openBalena backends may be updated in a way that is no longer compatible
with this CLI version.`; with this CLI version.`;
} }
/** Disable deprecation checks (for the benefit of code testing). */
public static disable() {
DeprecationChecker.disabled = true;
}
/** Re-enable deprecation checks (for the benefit of code testing). */
public static enable() {
DeprecationChecker.disabled = false;
}
} }

View File

@ -23,8 +23,8 @@ setEsVersion('es2018');
process.env.BALENARC_NO_SENTRY = '1'; process.env.BALENARC_NO_SENTRY = '1';
// Disable deprecation checks while running test code // Disable deprecation checks while running test code
import { DeprecationChecker } from '../build/deprecation'; // Like the global `--unsupported` flag
DeprecationChecker.disable(); process.env.BALENARC_UNSUPPORTED = '1';
import * as tmp from 'tmp'; import * as tmp from 'tmp';
tmp.setGracefulCleanup(); tmp.setGracefulCleanup();

View File

@ -54,10 +54,16 @@ describe('DeprecationChecker', function () {
Parameters<typeof mockStorage.set>, Parameters<typeof mockStorage.set>,
ReturnType<typeof mockStorage.set> ReturnType<typeof mockStorage.set>
>; >;
let originalUnsupported: string | undefined;
// see also DeprecationChecker.disable() in tests/config-tests.ts this.beforeAll(() => {
this.beforeAll(() => DeprecationChecker.enable()); // Temporarily undo settings from `tests/config-tests.ts`
this.afterAll(() => DeprecationChecker.disable()); originalUnsupported = process.env.BALENARC_UNSUPPORTED;
delete process.env.BALENARC_UNSUPPORTED;
});
this.afterAll(() => {
process.env.BALENARC_UNSUPPORTED = originalUnsupported;
});
this.beforeEach(() => { this.beforeEach(() => {
npm = new NpmMock(); npm = new NpmMock();