From 1aebe35df4de67739513b1f2bec3e3e2eec481e9 Mon Sep 17 00:00:00 2001 From: Albert Tregnaghi Date: Fri, 13 Dec 2024 12:37:52 +0100 Subject: [PATCH] Avoid unnecessary output in tests #3735 --- .../__test__/client-version-helper.test.ts | 16 ++++++++++++++ .../__test__/configuration-builder.test.ts | 21 ++++++++++++------- .../scan/__test__/init-scan.test.ts | 16 ++++++++++++++ .../scan/__test__/shell-arg-sanitizer.test.ts | 21 ++++++++++++++++++- 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/github-actions/scan/__test__/client-version-helper.test.ts b/github-actions/scan/__test__/client-version-helper.test.ts index d8227497b2..72f9052e4f 100644 --- a/github-actions/scan/__test__/client-version-helper.test.ts +++ b/github-actions/scan/__test__/client-version-helper.test.ts @@ -3,6 +3,22 @@ import { getClientVersion } from '../src/client-version-helper'; import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; +import * as core from '@actions/core'; + +jest.mock('@actions/core'); + +const mockDebug = core.debug as jest.MockedFunction; + +const debugEnabled = false; + +beforeEach(() => { + mockDebug.mockImplementation((message: string | Error) => { + if (debugEnabled) { + console.log(`Debug: ${message}`); + } + }); + mockDebug.mockClear(); +}); describe('getClientVersion', function () { diff --git a/github-actions/scan/__test__/configuration-builder.test.ts b/github-actions/scan/__test__/configuration-builder.test.ts index 2787db9307..1b8e238e5d 100644 --- a/github-actions/scan/__test__/configuration-builder.test.ts +++ b/github-actions/scan/__test__/configuration-builder.test.ts @@ -6,7 +6,12 @@ import { SecHubConfigurationModelBuilderData } from '../src/configuration-builde jest.mock('@actions/core'); -function dumpModel(model: SecHubConfigurationModel){ +const debugEnabled = false; + +function logDebug(model: SecHubConfigurationModel){ + if (! debugEnabled){ + return; + } const json = JSON.stringify(model, null, 2); // pretty printed output console.log('json='+json); @@ -34,7 +39,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); @@ -66,7 +71,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); @@ -101,7 +106,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); @@ -138,7 +143,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); @@ -172,7 +177,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); @@ -206,7 +211,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); @@ -241,7 +246,7 @@ describe('configuration-builder', function() { const model= configBuilder.createSecHubConfigurationModel(builderData); /* test */ - dumpModel(model); + logDebug(model); expect(model.apiVersion).toEqual('1.0'); diff --git a/github-actions/scan/__test__/init-scan.test.ts b/github-actions/scan/__test__/init-scan.test.ts index 94c3fa99f9..16737206ea 100644 --- a/github-actions/scan/__test__/init-scan.test.ts +++ b/github-actions/scan/__test__/init-scan.test.ts @@ -5,6 +5,22 @@ import {initReportFormats, initSecHubJson} from '../src/init-scan'; jest.mock('./../src/configuration-builder'); import {SecHubConfigurationModelBuilderData, createSecHubConfigJsonFile} from '../src/configuration-builder'; +import * as core from '@actions/core'; + +jest.mock('@actions/core'); + +const mockInfo = core.info as jest.MockedFunction; + +const debugEnabled = false; + +beforeEach(() => { + mockInfo.mockImplementation((message: string | Error) => { + if (debugEnabled) { + console.log(`Info: ${message}`); + } + }); + mockInfo.mockClear(); +}); describe('initSecHubJson', function () { it('throws error if configPath is set, but file does not exist', function () { diff --git a/github-actions/scan/__test__/shell-arg-sanitizer.test.ts b/github-actions/scan/__test__/shell-arg-sanitizer.test.ts index f03ed1a19d..bd4c5021f8 100644 --- a/github-actions/scan/__test__/shell-arg-sanitizer.test.ts +++ b/github-actions/scan/__test__/shell-arg-sanitizer.test.ts @@ -1,8 +1,27 @@ +/* eslint-disable indent */ // SPDX-License-Identifier: MIT import * as shellArgSanitizer from '../src/shell-arg-sanitizer'; +import * as core from '@actions/core'; + +jest.mock('@actions/core'); + +const mockError = core.error as jest.MockedFunction; + +const debugEnabled = false; + +beforeEach(() => { + mockError.mockImplementation((message: string | Error) => { + if (debugEnabled) { + console.log(`Error: ${message}`); + } + }); + mockError.mockClear(); +}); + describe('sanitize', () => { + test.each([ ['rm -rf /; echo hacked'], // Command chaining ['echo $(whoami)'], // Command substitution @@ -69,7 +88,7 @@ describe('sanitize', () => { (arg) => { /* test */ expect(() => shellArgSanitizer.sanitize(arg)).not.toThrow(); - }); + }); it('removes whitespaces', function () { /* prepare */