Skip to content

Commit

Permalink
Avoid unnecessary output in tests #3735
Browse files Browse the repository at this point in the history
  • Loading branch information
de-jcup committed Dec 13, 2024
1 parent c5a0146 commit 1aebe35
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
16 changes: 16 additions & 0 deletions github-actions/scan/__test__/client-version-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof core.debug>;

const debugEnabled = false;

beforeEach(() => {
mockDebug.mockImplementation((message: string | Error) => {
if (debugEnabled) {
console.log(`Debug: ${message}`);
}
});
mockDebug.mockClear();
});

describe('getClientVersion', function () {

Expand Down
21 changes: 13 additions & 8 deletions github-actions/scan/__test__/configuration-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -34,7 +39,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down Expand Up @@ -66,7 +71,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down Expand Up @@ -101,7 +106,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down Expand Up @@ -138,7 +143,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down Expand Up @@ -172,7 +177,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down Expand Up @@ -206,7 +211,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down Expand Up @@ -241,7 +246,7 @@ describe('configuration-builder', function() {
const model= configBuilder.createSecHubConfigurationModel(builderData);

/* test */
dumpModel(model);
logDebug(model);

expect(model.apiVersion).toEqual('1.0');

Expand Down
16 changes: 16 additions & 0 deletions github-actions/scan/__test__/init-scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof core.info>;

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 () {
Expand Down
21 changes: 20 additions & 1 deletion github-actions/scan/__test__/shell-arg-sanitizer.test.ts
Original file line number Diff line number Diff line change
@@ -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<typeof core.error>;

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
Expand Down Expand Up @@ -69,7 +88,7 @@ describe('sanitize', () => {
(arg) => {
/* test */
expect(() => shellArgSanitizer.sanitize(arg)).not.toThrow();
});
});

it('removes whitespaces', function () {
/* prepare */
Expand Down

0 comments on commit 1aebe35

Please sign in to comment.