Skip to content

Commit

Permalink
Add log level
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroki0525 committed Nov 23, 2023
1 parent 3a9b95b commit 3aad917
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 141 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "git",
"url": "https://github.com/hiroki0525/dandori.git"
},
"type": "module",
"scripts": {
"build": "turbo build",
"prepare": "husky install",
Expand Down
14 changes: 10 additions & 4 deletions packages/cli/src/core/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import generateDandoriTasks, {
OptionalTaskPropsOption,
} from "@dandori/core";

const { actualLoadFile } = await vi.hoisted(async () => {
const actualModule = await import("@dandori/libs");
return {
actualLoadFile: actualModule.loadFile,
};
});

const tasks: DandoriTask[] = [
{
id: "1",
Expand All @@ -32,14 +39,13 @@ vi.mock("@dandori/core", () => ({

const mockLogError = vi.fn();

vi.mock("@dandori/libs", async () => {
const actualModule =
await vi.importActual<typeof import("@dandori/libs")>("@dandori/libs");
vi.mock("@dandori/libs", () => {
return {
...actualModule,
logLevel: "info",
getLogger: vi.fn(() => ({
error: mockLogError,
})),
loadFile: actualLoadFile,
};
});

Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ vi.mock("openai", () => {

const mockLogDebug = vi.fn();

vi.mock("@dandori/libs", async () => {
const actualModule =
await vi.importActual<typeof import("@dandori/libs")>("@dandori/libs");
vi.mock("@dandori/libs", () => {
return {
...actualModule,
logLevel: "info",
getLogger: vi.fn(() => ({
error: vi.fn(),
debug: mockLogDebug,
Expand Down
9 changes: 9 additions & 0 deletions packages/libs/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "process" {
global {
namespace NodeJS {
interface ProcessEnv {
LOG_LEVEL: "debug" | "info" | "warn" | "error";
}
}
}
}
117 changes: 84 additions & 33 deletions packages/libs/src/__tests__/loadEnvFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,95 @@
import { describe, expect, vi, it, beforeEach } from "vitest";
import { getLogger, setLogger } from "../logger";
import pino from "pino";

const defaultLogger = pino({
name: "dandori",
level: "debug",
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});
import { loadEnvFile } from "../index";
import {
describe,
it,
expect,
beforeAll,
afterAll,
beforeEach,
vi,
afterEach,
} from "vitest";
import { mkdir, rm, rmdir, writeFile } from "fs/promises";

const mockErrorLog = vi.fn();

vi.mock("pino", () => ({
default: vi.fn(),
vi.mock("../logger", () => ({
getLogger: vi.fn(() => ({
error: mockErrorLog,
})),
}));

describe("logger", () => {
describe("use default logger", () => {
it("get default logger", () => {
expect(getLogger()).toEqual(defaultLogger);
describe("loadEnvFile", () => {
describe("with valid .env file", () => {
const openApiKeyPropName = "OPENAI_API_KEY";

afterEach(() => {
delete process.env[openApiKeyPropName];
});

describe("no filePath argument", () => {
const apiKey = "123";
const envFileName = ".env";

beforeAll(async () => {
await writeFile(envFileName, `${openApiKeyPropName}=${apiKey}`);
});

afterAll(async () => {
await rm(envFileName);
});

beforeEach(() => {
loadEnvFile();
});

it(`loaded ${openApiKeyPropName}`, () => {
expect(process.env[openApiKeyPropName]).toBe(apiKey);
});
});

describe("filePath argument", () => {
const apiKey = "456";
const envFileDir = "./dir";
const envFilePath = `./${envFileDir}/.env`;

beforeAll(async () => {
await mkdir(envFileDir);
await writeFile(envFilePath, `${openApiKeyPropName}=${apiKey}`);
});

afterAll(async () => {
await rm(envFilePath);
await rmdir(envFileDir);
});

beforeEach(() => {
loadEnvFile(envFilePath);
});

it(`loaded ${openApiKeyPropName}`, () => {
expect(process.env[openApiKeyPropName]).toBe(apiKey);
});
});
});

describe("use custom logger", () => {
const customLogger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
customProp: vi.fn(),
};

beforeEach(() => {
setLogger(customLogger);
describe("without valid .env file", () => {
const runErrorLoadEnvFile = () => loadEnvFile("./nodir/.env");

afterEach(() => {
vi.clearAllMocks();
});

it("throw Error", async () => {
expect(runErrorLoadEnvFile).toThrowError();
});

it("get custom logger", () => {
expect(getLogger()).toEqual(customLogger);
it("called error log", async () => {
try {
runErrorLoadEnvFile();
} catch (e) {
expect(mockErrorLog).toBeCalled();
}
});
});
});
117 changes: 33 additions & 84 deletions packages/libs/src/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,95 +1,44 @@
import { loadEnvFile } from "../index";
import {
describe,
it,
expect,
beforeAll,
afterAll,
beforeEach,
vi,
afterEach,
} from "vitest";
import { mkdir, rm, rmdir, writeFile } from "fs/promises";

const mockErrorLog = vi.fn();
import { describe, expect, vi, it, beforeEach } from "vitest";
import { getLogger, setLogger } from "../logger";
import pino from "pino";

const defaultLogger = pino({
name: "dandori",
level: "debug",
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});

vi.mock("../logger", () => ({
getLogger: vi.fn(() => ({
error: mockErrorLog,
})),
vi.mock("pino", () => ({
default: vi.fn(),
}));

describe("loadEnvFile", () => {
describe("with valid .env file", () => {
const openApiKeyPropName = "OPENAI_API_KEY";

afterEach(() => {
delete process.env[openApiKeyPropName];
});

describe("no filePath argument", () => {
const apiKey = "123";
const envFileName = ".env";

beforeAll(async () => {
await writeFile(envFileName, `${openApiKeyPropName}=${apiKey}`);
});

afterAll(async () => {
await rm(envFileName);
});

beforeEach(() => {
loadEnvFile();
});

it(`loaded ${openApiKeyPropName}`, () => {
expect(process.env[openApiKeyPropName]).toBe(apiKey);
});
});

describe("filePath argument", () => {
const apiKey = "456";
const envFileDir = "./dir";
const envFilePath = `./${envFileDir}/.env`;

beforeAll(async () => {
await mkdir(envFileDir);
await writeFile(envFilePath, `${openApiKeyPropName}=${apiKey}`);
});

afterAll(async () => {
await rm(envFilePath);
await rmdir(envFileDir);
});

beforeEach(() => {
loadEnvFile(envFilePath);
});

it(`loaded ${openApiKeyPropName}`, () => {
expect(process.env[openApiKeyPropName]).toBe(apiKey);
});
describe("logger", () => {
describe("use default logger", () => {
it("get default logger", () => {
expect(getLogger()).toEqual(defaultLogger);
});
});

describe("without valid .env file", () => {
const runErrorLoadEnvFile = () => loadEnvFile("./nodir/.env");

afterEach(() => {
vi.clearAllMocks();
});

it("throw Error", async () => {
expect(runErrorLoadEnvFile).toThrowError();
describe("use custom logger", () => {
const customLogger = {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
customProp: vi.fn(),
};

beforeEach(() => {
setLogger(customLogger);
});

it("called error log", async () => {
try {
runErrorLoadEnvFile();
} catch (e) {
expect(mockErrorLog).toBeCalled();
}
it("get custom logger", () => {
expect(getLogger()).toEqual(customLogger);
});
});
});
4 changes: 3 additions & 1 deletion packages/libs/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export const setLogger = (newLogger: Logger): void => {
logger = newLogger;
};

export const logLevel = process.env.LOG_LEVEL ?? "info";

export const getLogger = (): Logger => {
if (!logger) {
setLogger(
pino({
name: "dandori",
level: "debug",
level: logLevel,
transport: {
target: "pino-pretty",
options: {
Expand Down
3 changes: 2 additions & 1 deletion packages/libs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"extends": "../../tsconfig.json"
"extends": "../../tsconfig.json",
"include": ["./src/**/*", "global.d.ts"]
}
21 changes: 12 additions & 9 deletions packages/ui/src/__tests__/miro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ vi.mock("@mirohq/miro-api", () => {

const mockLogInfo = vi.fn();

vi.mock("@dandori/libs", () => ({
getLogger: vi.fn(() => ({
debug: vi.fn(),
info: mockLogInfo,
})),
runPromisesSequentially: vi.fn((runPromises, _runningLogPrefix) =>
Promise.all(runPromises.map((runPromise: () => any) => runPromise())),
),
}));
vi.mock("@dandori/libs", () => {
return {
logLevel: "info",
getLogger: vi.fn(() => ({
debug: vi.fn(),
info: mockLogInfo,
})),
runPromisesSequentially: vi.fn((runPromises, _runningLogPrefix) =>
Promise.all(runPromises.map((runPromise: () => any) => runPromise())),
),
};
});

const mockRunPromisesSequentially = runPromisesSequentially as Mock;

Expand Down
Loading

0 comments on commit 3aad917

Please sign in to comment.