Skip to content

Commit

Permalink
refactor(cli-logger): code and tests for log file path generator (#13088
Browse files Browse the repository at this point in the history
)

* refactor(amplify-cli-logger): tests for baseLogFilePath

* refactor(amplify-cli-logger): simplify baseLogFilePath test

* refactor: log file path code and tests

* refactor: use old filename for tests

* refactor: move spy inside the test suite

* refactor: change project path var name

* refactor: rename file
  • Loading branch information
mzarnawski authored Sep 5, 2023
1 parent 066d4ed commit fe043d5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 93 deletions.
34 changes: 0 additions & 34 deletions packages/amplify-cli-logger/src/__tests__/TestBasePath.test.ts

This file was deleted.

34 changes: 0 additions & 34 deletions packages/amplify-cli-logger/src/__tests__/TestFilePath.test.ts

This file was deleted.

57 changes: 57 additions & 0 deletions packages/amplify-cli-logger/src/__tests__/getLogFilePath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import path from 'path';
import os from 'os';
import { constants as c } from '../constants';
import { getLogFilePath, getLocalLogFilePath, getLogAuditFilePath, getLocalAuditLogFile } from '../getLogFilePath';

describe('test log file path creation', () => {
const slash = path.sep;
const projectPath = 'myProj';
const homeDir = 'home';

jest.spyOn(os, 'homedir').mockReturnValue(homeDir);

afterAll(() => {
jest.restoreAllMocks();
});

it('log audit file in a specified directory', () => {
const result = getLocalAuditLogFile(projectPath);
const expected = projectPath + slash + c.LOG_DIRECTORY + slash + c.LOG_AUDIT_FOLDER + slash + c.LOG_AUDIT_FILENAME;
expect(result).toBe(expected);
});

it('log file in a specified directory', () => {
const result = getLocalLogFilePath(projectPath);
const expected = projectPath + slash + c.LOG_DIRECTORY + slash + c.LOG_FILENAME;
expect(result).toBe(expected);
});

it('log audit file in home directory', () => {
jest.replaceProperty(process, 'argv', ['node', '']);
const result = getLogAuditFilePath();
const expected = homeDir + slash + c.DOT_AMPLIFY + slash + c.LOG_DIRECTORY + slash + c.LOG_AUDIT_FOLDER + slash + c.LOG_AUDIT_FILENAME;
expect(result).toBe(expected);
});

it('log file in home directory', () => {
jest.replaceProperty(process, 'argv', ['node', '']);
const result = getLogFilePath();
const expected = homeDir + slash + c.DOT_AMPLIFY + slash + c.LOG_DIRECTORY + slash + c.LOG_FILENAME;
expect(result).toBe(expected);
});

it('logs-dev folder in home directory for dev build audit file', () => {
jest.replaceProperty(process, 'argv', ['node', 'dev']);
const result = getLogAuditFilePath();
const expected =
homeDir + slash + c.DOT_AMPLIFY + slash + c.LOG_DIRECTORY + '-dev' + slash + c.LOG_AUDIT_FOLDER + slash + c.LOG_AUDIT_FILENAME;
expect(result).toBe(expected);
});

it('logs-dev folder in home directory for dev build log file', () => {
jest.replaceProperty(process, 'argv', ['node', 'dev']);
const result = getLogFilePath();
const expected = homeDir + slash + c.DOT_AMPLIFY + slash + c.LOG_DIRECTORY + '-dev' + slash + c.LOG_FILENAME;
expect(result).toBe(expected);
});
});
24 changes: 0 additions & 24 deletions packages/amplify-cli-logger/src/baseLogFilePath.ts

This file was deleted.

23 changes: 22 additions & 1 deletion packages/amplify-cli-logger/src/getLogFilePath.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import path from 'path';
import os from 'os';
import { constants } from './constants';
import { getLogDirectory, getLocalLogFileDirectory } from './baseLogFilePath';

function getFolder() {
let folder = constants.LOG_DIRECTORY;

if (process.argv.length > 1) {
const executable = process.argv[1];

if (executable && executable.includes('dev')) {
folder += '-dev';
}
}
return folder;
}

function getLocalLogFileDirectory(projectPath: string): string {
return path.join(projectPath, constants.LOG_DIRECTORY);
}

function getLogDirectory(): string {
return path.join(os.homedir(), constants.DOT_AMPLIFY, getFolder());
}

export function getLogFilePath(): string {
return path.join(getLogDirectory(), constants.LOG_FILENAME);
Expand Down

0 comments on commit fe043d5

Please sign in to comment.