Skip to content

Commit

Permalink
test: ut for env global env/mode changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Dec 29, 2023
1 parent 2934091 commit ae99dff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
35 changes: 28 additions & 7 deletions test/unit/globalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,43 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect } from 'chai';
import { isString } from '@salesforce/ts-types';
import { Global, Mode } from '../../src/global';

describe('Global', () => {
describe('environmentMode', () => {
const original = { SFDX_ENV: process.env.SFDX_ENV, SF_ENV: process.env.SF_ENV };
const originalEnv = { SFDX_ENV: process.env.SFDX_ENV, SF_ENV: process.env.SF_ENV };
const cleanEnv = () => Object.keys(originalEnv).map((key) => delete process.env[key]);

beforeEach(() => {
delete process.env.SFDX_ENV;
delete process.env.SF_ENV;
cleanEnv();
});

after(() => {
process.env.SFDX_ENV = original.SFDX_ENV;
process.env.SF_ENV = original.SF_ENV;
cleanEnv();
Object.entries(originalEnv)
.filter(([, value]) => isString(value))
.map(([key, value]) => {
process.env[key] = value;
});
});

it('uses SFDX_ENV mode', () => {
it('uses SFDX_ENV mode alone', () => {
process.env.SFDX_ENV = 'development';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.true;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.false;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('uses SF_ENV mode alone', () => {
process.env.SF_ENV = 'development';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.true;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.false;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('prefers SF_ENV mode', () => {
process.env.SF_ENV = 'test';
process.env.SFDX_ENV = 'development';
Expand Down Expand Up @@ -69,12 +82,20 @@ describe('Global', () => {
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('defaults to production when invalid values are specified', () => {
it('defaults to production when invalid values are specified (SFDX)', () => {
process.env.SFDX_ENV = 'notARealMode';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.true;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});

it('defaults to production when invalid values are specified (SF)', () => {
process.env.SF_ENV = 'notARealMode';
expect(Global.getEnvironmentMode() === Mode.DEVELOPMENT).to.be.false;
expect(Global.getEnvironmentMode() === Mode.PRODUCTION).to.be.true;
expect(Global.getEnvironmentMode() === Mode.DEMO).to.be.false;
expect(Global.getEnvironmentMode() === Mode.TEST).to.be.false;
});
});
});
39 changes: 24 additions & 15 deletions test/unit/loggerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */

import { expect, config as chaiConfig } from 'chai';
import { isString } from '@salesforce/ts-types';
import { Logger, LoggerLevel, computeLevel } from '../../src/logger/logger';
import { shouldThrowSync, TestContext } from '../../src/testSetup';

Expand All @@ -18,12 +19,20 @@ import { shouldThrowSync, TestContext } from '../../src/testSetup';
chaiConfig.truncateThreshold = 0;
describe('Logger', () => {
const $$ = new TestContext();
const sfdxEnv = process.env.SFDX_ENV;
const logRotationPeriodBackup = process.env.SF_LOG_ROTATION_PERIOD;
const logRotationCountBackup = process.env.SF_LOG_ROTATION_COUNT;

beforeEach(async () => {
const originalEnv = {
SFDX_ENV: process.env.SFDX_ENV,
SF_ENV: process.env.SF_ENV,
SF_LOG_LEVEL: process.env.SF_LOG_LEVEL,
SF_DISABLE_LOG_FILE: process.env.SF_DISABLE_LOG_FILE,
SF_LOG_ROTATION_PERIOD: process.env.SF_LOG_ROTATION_PERIOD,
SF_LOG_ROTATION_COUNT: process.env.SF_LOG_ROTATION_COUNT,
};

const cleanEnv = () => Object.keys(originalEnv).map((key) => delete process.env[key]);
beforeEach(() => {
cleanEnv();
process.env.SFDX_ENV = 'test';
process.env.SF_ENV = 'test';

// Must restore the globally stubbed Logger.child method here. Stubbed in testSetup.
// @ts-expect-error: called is a sinon spy property
Expand All @@ -32,9 +41,15 @@ describe('Logger', () => {

afterEach(() => {
Logger.destroyRoot();
if (sfdxEnv) process.env.SFDX_ENV = sfdxEnv;
if (logRotationPeriodBackup) process.env.SF_LOG_ROTATION_PERIOD = logRotationPeriodBackup;
if (logRotationCountBackup) process.env.SF_LOG_ROTATION_COUNT = logRotationCountBackup;
});

after(() => {
cleanEnv();
Object.entries(originalEnv)
.filter(([, value]) => isString(value))
.map(([key, value]) => {
process.env[key] = value;
});
});

describe('constructor', () => {
Expand All @@ -47,13 +62,10 @@ describe('Logger', () => {
});

describe('DISABLE_LOG_FILE', () => {
const LOG_FILES_DISABLED = process.env.SF_DISABLE_LOG_FILE;
before(() => {
process.env.SF_DISABLE_LOG_FILE = 'true';
});
after(() => {
process.env.SF_DISABLE_LOG_FILE = LOG_FILES_DISABLED;
});

it('should construct a new named logger', async () => {
const logger1 = new Logger({ name: 'testLogger-noop' });
expect(logger1).to.be.instanceof(Logger);
Expand All @@ -66,9 +78,6 @@ describe('Logger', () => {

describe('levels', () => {
describe('level computation', () => {
afterEach(() => {
delete process.env.SF_LOG_LEVEL;
});
it('should use a matching a level name when passed in', () => {
expect(computeLevel('error')).to.equal('error');
});
Expand Down

2 comments on commit ae99dff

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: ae99dff Previous: c20bba8 Ratio
Child logger creation 476097 ops/sec (±0.48%) 470268 ops/sec (±0.81%) 0.99
Logging a string on root logger 825412 ops/sec (±9.51%) 787763 ops/sec (±8.22%) 0.95
Logging an object on root logger 657717 ops/sec (±8.58%) 601125 ops/sec (±6.11%) 0.91
Logging an object with a message on root logger 5575 ops/sec (±217.38%) 8442 ops/sec (±205.69%) 1.51
Logging an object with a redacted prop on root logger 443406 ops/sec (±8.81%) 379533 ops/sec (±22.05%) 0.86
Logging a nested 3-level object on root logger 369545 ops/sec (±7.88%) 396344 ops/sec (±6.72%) 1.07

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: ae99dff Previous: c20bba8 Ratio
Child logger creation 316747 ops/sec (±1.07%) 318206 ops/sec (±0.96%) 1.00
Logging a string on root logger 769846 ops/sec (±4.82%) 790067 ops/sec (±5.83%) 1.03
Logging an object on root logger 629654 ops/sec (±7.14%) 588717 ops/sec (±7.51%) 0.93
Logging an object with a message on root logger 5228 ops/sec (±208.55%) 5018 ops/sec (±208.40%) 0.96
Logging an object with a redacted prop on root logger 435918 ops/sec (±15.42%) 458424 ops/sec (±17.42%) 1.05
Logging a nested 3-level object on root logger 340279 ops/sec (±5.23%) 336817 ops/sec (±5.57%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.