mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10:49 +00:00
Allow to configure loglevel through an env variable
This commit is contained in:
parent
6d2d943e18
commit
3573ef2bc5
3 changed files with 137 additions and 21 deletions
|
|
@ -25,3 +25,12 @@ export function getNpmCustomRegistries() {
|
||||||
export function getPipCustomRegistries() {
|
export function getPipCustomRegistries() {
|
||||||
return process.env.SAFE_CHAIN_PIP_CUSTOM_REGISTRIES;
|
return process.env.SAFE_CHAIN_PIP_CUSTOM_REGISTRIES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the logging level from environment variable
|
||||||
|
* Valid values: "silent", "normal", "verbose"
|
||||||
|
* @returns {string | undefined}
|
||||||
|
*/
|
||||||
|
export function getLoggingLevel() {
|
||||||
|
return process.env.SAFE_CHAIN_LOGGING;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,20 @@ export const LOGGING_NORMAL = "normal";
|
||||||
export const LOGGING_VERBOSE = "verbose";
|
export const LOGGING_VERBOSE = "verbose";
|
||||||
|
|
||||||
export function getLoggingLevel() {
|
export function getLoggingLevel() {
|
||||||
const level = cliArguments.getLoggingLevel();
|
// Priority 1: CLI argument
|
||||||
|
const cliLevel = cliArguments.getLoggingLevel();
|
||||||
if (level === LOGGING_SILENT) {
|
if (cliLevel === LOGGING_SILENT || cliLevel === LOGGING_VERBOSE) {
|
||||||
return LOGGING_SILENT;
|
return cliLevel;
|
||||||
|
}
|
||||||
|
if (cliLevel) {
|
||||||
|
// CLI arg was set but invalid, default to normal
|
||||||
|
return LOGGING_NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (level === LOGGING_VERBOSE) {
|
// Priority 2: Environment variable
|
||||||
return LOGGING_VERBOSE;
|
const envLevel = environmentVariables.getLoggingLevel()?.toLowerCase();
|
||||||
|
if (envLevel === LOGGING_SILENT || envLevel === LOGGING_VERBOSE) {
|
||||||
|
return envLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
return LOGGING_NORMAL;
|
return LOGGING_NORMAL;
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,15 @@ mock.module("fs", {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const { getNpmCustomRegistries, getPipCustomRegistries } = await import(
|
const {
|
||||||
"./settings.js"
|
getNpmCustomRegistries,
|
||||||
);
|
getPipCustomRegistries,
|
||||||
|
getLoggingLevel,
|
||||||
|
LOGGING_SILENT,
|
||||||
|
LOGGING_NORMAL,
|
||||||
|
LOGGING_VERBOSE,
|
||||||
|
} = await import("./settings.js");
|
||||||
|
const { initializeCliArguments } = await import("./cliArguments.js");
|
||||||
|
|
||||||
for (const { packageManager, getCustomRegistries, envVarName } of [
|
for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +32,7 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
getCustomRegistries: getPipCustomRegistries,
|
getCustomRegistries: getPipCustomRegistries,
|
||||||
envVarName: "SAFE_CHAIN_PIP_CUSTOM_REGISTRIES",
|
envVarName: "SAFE_CHAIN_PIP_CUSTOM_REGISTRIES",
|
||||||
},
|
},
|
||||||
])
|
]) {
|
||||||
{
|
|
||||||
describe(getCustomRegistries.name, async () => {
|
describe(getCustomRegistries.name, async () => {
|
||||||
let originalEnv;
|
let originalEnv;
|
||||||
|
|
||||||
|
|
@ -55,7 +60,10 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
it("should return registries without protocol", () => {
|
it("should return registries without protocol", () => {
|
||||||
configFileContent = JSON.stringify({
|
configFileContent = JSON.stringify({
|
||||||
[packageManager]: {
|
[packageManager]: {
|
||||||
customRegistries: [`${packageManager}.company.com`, "registry.internal.net"],
|
customRegistries: [
|
||||||
|
`${packageManager}.company.com`,
|
||||||
|
"registry.internal.net",
|
||||||
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -143,8 +151,7 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
|
|
||||||
it("should parse comma-separated registries from environment variable", () => {
|
it("should parse comma-separated registries from environment variable", () => {
|
||||||
delete process.env[envVarName];
|
delete process.env[envVarName];
|
||||||
process.env[envVarName] =
|
process.env[envVarName] = "env1.registry.com,env2.registry.net";
|
||||||
"env1.registry.com,env2.registry.net";
|
|
||||||
configFileContent = undefined;
|
configFileContent = undefined;
|
||||||
|
|
||||||
const registries = getCustomRegistries();
|
const registries = getCustomRegistries();
|
||||||
|
|
@ -157,8 +164,7 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
|
|
||||||
it("should trim whitespace from environment variable registries", () => {
|
it("should trim whitespace from environment variable registries", () => {
|
||||||
delete process.env[envVarName];
|
delete process.env[envVarName];
|
||||||
process.env[envVarName] =
|
process.env[envVarName] = " env1.registry.com , env2.registry.net ";
|
||||||
" env1.registry.com , env2.registry.net ";
|
|
||||||
configFileContent = undefined;
|
configFileContent = undefined;
|
||||||
|
|
||||||
const registries = getCustomRegistries();
|
const registries = getCustomRegistries();
|
||||||
|
|
@ -188,11 +194,15 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
|
|
||||||
it("should remove duplicate registries when merging env and config", () => {
|
it("should remove duplicate registries when merging env and config", () => {
|
||||||
delete process.env[envVarName];
|
delete process.env[envVarName];
|
||||||
process.env[envVarName] =
|
process.env[
|
||||||
`${packageManager}.company.com,env.registry.com`;
|
envVarName
|
||||||
|
] = `${packageManager}.company.com,env.registry.com`;
|
||||||
configFileContent = JSON.stringify({
|
configFileContent = JSON.stringify({
|
||||||
[packageManager]: {
|
[packageManager]: {
|
||||||
customRegistries: [`${packageManager}.company.com`, "config.registry.net"],
|
customRegistries: [
|
||||||
|
`${packageManager}.company.com`,
|
||||||
|
"config.registry.net",
|
||||||
|
],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -221,8 +231,7 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
|
|
||||||
it("should handle empty strings in comma-separated list", () => {
|
it("should handle empty strings in comma-separated list", () => {
|
||||||
delete process.env[envVarName];
|
delete process.env[envVarName];
|
||||||
process.env[envVarName] =
|
process.env[envVarName] = "env1.registry.com,,env2.registry.net,";
|
||||||
"env1.registry.com,,env2.registry.net,";
|
|
||||||
configFileContent = undefined;
|
configFileContent = undefined;
|
||||||
|
|
||||||
const registries = getCustomRegistries();
|
const registries = getCustomRegistries();
|
||||||
|
|
@ -264,3 +273,95 @@ for (const { packageManager, getCustomRegistries, envVarName } of [
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe("getLoggingLevel", () => {
|
||||||
|
let originalEnv;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
originalEnv = process.env.SAFE_CHAIN_LOGGING;
|
||||||
|
delete process.env.SAFE_CHAIN_LOGGING;
|
||||||
|
// Reset CLI arguments state
|
||||||
|
initializeCliArguments([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
if (originalEnv !== undefined) {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = originalEnv;
|
||||||
|
} else {
|
||||||
|
delete process.env.SAFE_CHAIN_LOGGING;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return normal by default when nothing is configured", () => {
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_NORMAL);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return silent from environment variable", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "silent";
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_SILENT);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return verbose from environment variable", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "verbose";
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_VERBOSE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle uppercase environment variable values", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "VERBOSE";
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_VERBOSE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle mixed case environment variable values", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "Silent";
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_SILENT);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return normal for invalid environment variable values", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "invalid";
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_NORMAL);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should prioritize CLI argument over environment variable", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "verbose";
|
||||||
|
initializeCliArguments(["--safe-chain-logging=silent"]);
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_SILENT);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should use environment variable when CLI argument is not set", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "silent";
|
||||||
|
initializeCliArguments(["install", "express"]);
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_SILENT);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return normal when CLI argument is invalid (even if env var is valid)", () => {
|
||||||
|
process.env.SAFE_CHAIN_LOGGING = "verbose";
|
||||||
|
initializeCliArguments(["--safe-chain-logging=invalid"]);
|
||||||
|
|
||||||
|
const level = getLoggingLevel();
|
||||||
|
|
||||||
|
assert.strictEqual(level, LOGGING_NORMAL);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue