Skip to content

Commit

Permalink
fix: add tests to replacement logic configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTowles committed Sep 26, 2022
1 parent a6a3964 commit 92681c9
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 103 deletions.
146 changes: 137 additions & 9 deletions src/configuration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from 'vitest'
import { WorkspaceConfiguration, ConfigurationTarget } from 'vscode';
import { Constants } from "./constants";
import { parseConfigurationToConfig } from "./configuration";

import { parseConfigurationToConfig, replacePathVariable } from "./configuration";
import path from 'path';


class WorkspaceConfigurationMock implements WorkspaceConfiguration {
Expand All @@ -18,9 +18,7 @@ class WorkspaceConfigurationMock implements WorkspaceConfiguration {
}
get<T>(section: string, defaultValue?: T): T | undefined {

if(defaultValue !== undefined) {
throw new Error("Logic for defaultValue not implemented.");
}


let result: string | boolean;

Expand Down Expand Up @@ -64,6 +62,7 @@ class WorkspaceConfigurationMock implements WorkspaceConfiguration {
default:
throw new Error(`No Mock provided for Config ${section}.`);
}

return result as T;
}
}
Expand All @@ -81,16 +80,145 @@ describe('Configuration', () => {
const vsWorkspaceConfiguration = new WorkspaceConfigurationMock();
const config = parseConfigurationToConfig({
projectPath: __dirname,
filePath: __dirname,
editorOpenFilePath: __dirname,
configuration: vsWorkspaceConfiguration,
})

expect(config.basePathConfig).toBe(__dirname);
expect(config.suffixConfig).toBe('');
//expect(config.forceUnixStyleSeparatorConfig).toBeTruthy();

//expect(config.showFilePathConfirmInputBox).toBeTruthy();


})


it('replacePathVariable - no replacement done', () => {
const originalPath = `/dir1/dir2`
const editorFile = path.join(__dirname, 'test', 'test.md');
const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: __dirname,
})

// no replacement done
expect(alteredValue).toBe(originalPath);

})

it('replacePathVariable - replace with edit file\'s folder', () => {
const originalPath = '${currentFileDir}'
const editorFile = path.join(__dirname, 'test', 'test.md');
const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: __dirname,
})

// should replace with folder of editor file
expect(alteredValue).toBe(path.dirname(editorFile));

})


it('replacePathVariable - replace with edit file\'s folder and append another folder', () => {
const originalPath = '${currentFileDir}/test2Folder'
const editorFile = path.join(__dirname, 'test', 'test.md');
const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: __dirname,
})

// should replace with folder of editor file and append another folder
const targetFolder = path.join(path.dirname(editorFile), 'test2Folder');
expect(alteredValue).toBe(targetFolder);

})

it('replacePathVariable - replace with projectRoot', () => {
const originalPath = '${projectRoot}'
const editorFile = path.join(__dirname, 'test', 'test.md');
const productRoot = __dirname;

const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: productRoot,
})

// should replace with folder of editor file
expect(alteredValue).toBe(productRoot);
})

it('replacePathVariable - replace with projectRoot', () => {
const originalPath = '${projectRoot}'
const editorFile = path.join(__dirname, 'test', 'test.md');
const productRoot = __dirname;

const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: productRoot,
})

// should replace with folder of editor file
expect(alteredValue).toBe(productRoot);
})


it('replacePathVariable - replace with currentFileName', () => {
const originalPath = '${currentFileName}'
const editorFile = path.join(__dirname, 'test', 'test.md');
const productRoot = __dirname;

const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: productRoot,
})

// should replace with editor
expect(alteredValue).toBe(path.basename(editorFile));
})



it('replacePathVariable - replace with currentFileNameWithoutExt', () => {
const originalPath = '${currentFileNameWithoutExt}'
const editorFile = path.join(__dirname, 'test', 'test.md');
const productRoot = __dirname;

const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: productRoot,
})

let ext = path.extname(editorFile);
let fileNameWithoutExt = path.basename(editorFile, ext);

// should replace with editor filename without ext
expect(alteredValue).toBe(fileNameWithoutExt);
})

it('replacePathVariable - replace with projectRoot and currentFileNameWithoutExt', () => {
const originalPath = '${projectRoot}/${currentFileNameWithoutExt}.png'
const editorFile = path.join(__dirname, 'test', 'test.md');
const productRoot = __dirname;

const alteredValue = replacePathVariable({
pathStr: originalPath,
editorOpenFilePath: editorFile,
projectRoot: productRoot,
})

let ext = path.extname(editorFile);
let fileNameWithoutExt = path.basename(editorFile, ext);

// should replace with editor filename without ext
expect(alteredValue).toBe(path.join(productRoot, `${fileNameWithoutExt}.png`));
})



});
159 changes: 78 additions & 81 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,110 @@ import { WorkspaceConfiguration } from 'vscode';
import path from "path";
import { ILogger } from "./logger";

export interface Configuration {
export interface Configuration {
prefixConfig: string;
suffixConfig: string;
forceUnixStyleSeparatorConfig: boolean;
encodePathConfig: string;
namePrefixConfig: string;
nameSuffixConfig: string;
insertPatternConfig: string;
showFilePathConfirmInputBox: boolean,
filePathConfirmInputBoxMode: string;
defaultNameConfig: string;
folderPathConfig: string;
basePathConfig: string;
forceUnixStyleSeparatorConfig: boolean;
encodePathConfig: string;
namePrefixConfig: string;
nameSuffixConfig: string;
insertPatternConfig: string;
showFilePathConfirmInputBox: boolean,
filePathConfirmInputBoxMode: string;
defaultNameConfig: string;
folderPathConfig: string;
basePathConfig: string;

}

const PATH_VARIABLE_CURRENT_FILE_DIR = /\$\{currentFileDir\}/g;
const PATH_VARIABLE_PROJECT_ROOT = /\$\{projectRoot\}/g;
const PATH_VARIABLE_CURRENT_FILE_NAME = /\$\{currentFileName\}/g;
const PATH_VARIABLE_CURRENT_FILE_NAME_WITHOUT_EXT = /\$\{currentFileNameWithoutExt\}/g;




export const parseConfigurationToConfig = ({ projectPath, filePath, configuration }: { projectPath: string; filePath: string; configuration: WorkspaceConfiguration}): Configuration => {
export const parseConfigurationToConfig = ({ projectPath, editorOpenFilePath, configuration }: { projectPath: string; editorOpenFilePath: string; configuration: WorkspaceConfiguration }): Configuration => {

let defaultNameConfig = configuration.get<string>(Constants.Config_DefaultName);
let folderPathConfig = configuration.get<string>(Constants.Config_FolderPath);
let basePathConfig = configuration.get<string>(Constants.Config_BasePath );
let basePathConfig = configuration.get<string>(Constants.Config_BasePath);
let prefixConfig = configuration.get<string>(Constants.Config_Prefix, '');
let suffixConfig = configuration.get<string>(Constants.Config_Suffix, '');
let forceUnixStyleSeparatorConfig = configuration.get<boolean>(Constants.Config_ForceUnixStyleSeparator );
let encodePathConfig = configuration.get<string>(Constants.Config_EncodePath, '');
let namePrefixConfig = configuration.get<string>(Constants.Config_NamePrefix, '');
let nameSuffixConfig = configuration.get<string>(Constants.Config_NameSuffix, '');
let insertPatternConfig = configuration.get<string>(Constants.Config_InsertPattern, '');
let showFilePathConfirmInputBox= configuration.get<boolean>(Constants.Config_ShowFilePathConfirmInputBox) || false;
let filePathConfirmInputBoxMode = configuration.get<string>(Constants.Config_FilePathConfirmInputBoxMode) || "inputBox";
// load config pasteImage.defaultName
if (!defaultNameConfig) {
defaultNameConfig = "yyyy-LL-dd-HH-mm-ss"
}

// load config pasteImage.path
if (!folderPathConfig) {
folderPathConfig = "${currentFileDir}";
}
if (folderPathConfig.length !== folderPathConfig.trim().length) {
const errorMsg = `The config ${Constants.ConfigurationName}.path = '${folderPathConfig}' is invalid. please check your config.`;
throw new Error(errorMsg);
}
if (!basePathConfig) {
basePathConfig = "";
}

if (basePathConfig.length !== basePathConfig.trim().length) {
const errorMsg = `The config pasteImage.path = '${basePathConfig}' is invalid. please check your config.`;
throw new Error(errorMsg);
}
let forceUnixStyleSeparatorConfig = configuration.get<boolean>(Constants.Config_ForceUnixStyleSeparator);

let encodePathConfig = configuration.get<string>(Constants.Config_EncodePath, '');
let namePrefixConfig = configuration.get<string>(Constants.Config_NamePrefix, '');
let nameSuffixConfig = configuration.get<string>(Constants.Config_NameSuffix, '');
let insertPatternConfig = configuration.get<string>(Constants.Config_InsertPattern, '');
let showFilePathConfirmInputBox = configuration.get<boolean>(Constants.Config_ShowFilePathConfirmInputBox) || false;
let filePathConfirmInputBoxMode = configuration.get<string>(Constants.Config_FilePathConfirmInputBoxMode) || "inputBox";

// load config pasteImage.defaultName

if (!defaultNameConfig) {
defaultNameConfig = "yyyy-LL-dd-HH-mm-ss" //https://moment.github.io/luxon/#/formatting?id=table-of-tokens
}

// load config pasteImage.path
if (!folderPathConfig) {
folderPathConfig = "${currentFileDir}";
}
if (folderPathConfig.length !== folderPathConfig.trim().length) {
const errorMsg = `The config ${Constants.ConfigurationName}.path = '${folderPathConfig}' is invalid. please check your config.`;
throw new Error(errorMsg);
}

if (!basePathConfig) {
basePathConfig = "";
}

if (basePathConfig.length !== basePathConfig.trim().length) {
const errorMsg = `The config pasteImage.path = '${basePathConfig}' is invalid. please check your config.`;
throw new Error(errorMsg);
}

forceUnixStyleSeparatorConfig = !!forceUnixStyleSeparatorConfig;

defaultNameConfig = replacePathVariable(defaultNameConfig, projectPath, filePath, (x) => `[${x}]`);
folderPathConfig = replacePathVariable(folderPathConfig, projectPath, filePath);
basePathConfig = replacePathVariable(basePathConfig, projectPath, filePath);
namePrefixConfig = replacePathVariable(namePrefixConfig, projectPath, filePath);
nameSuffixConfig = replacePathVariable(nameSuffixConfig, projectPath, filePath);
insertPatternConfig = replacePathVariable(insertPatternConfig, projectPath, filePath);
defaultNameConfig = replacePathVariable({ pathStr: defaultNameConfig, projectRoot: projectPath, editorOpenFilePath: editorOpenFilePath });
folderPathConfig = replacePathVariable({ pathStr: folderPathConfig, projectRoot: projectPath, editorOpenFilePath: editorOpenFilePath });
basePathConfig = replacePathVariable({ pathStr: basePathConfig, projectRoot: projectPath, editorOpenFilePath: editorOpenFilePath });
namePrefixConfig = replacePathVariable({ pathStr: namePrefixConfig, projectRoot: projectPath, editorOpenFilePath: editorOpenFilePath });
nameSuffixConfig = replacePathVariable({ pathStr: nameSuffixConfig, projectRoot: projectPath, editorOpenFilePath: editorOpenFilePath });
insertPatternConfig = replacePathVariable({ pathStr: insertPatternConfig, projectRoot: projectPath, editorOpenFilePath: editorOpenFilePath });


const config: Configuration = {

// load other config
defaultNameConfig: defaultNameConfig,
prefixConfig: prefixConfig,
suffixConfig: suffixConfig,
forceUnixStyleSeparatorConfig: forceUnixStyleSeparatorConfig,
encodePathConfig: encodePathConfig,
namePrefixConfig: namePrefixConfig,
nameSuffixConfig: nameSuffixConfig,
insertPatternConfig: insertPatternConfig,
showFilePathConfirmInputBox: showFilePathConfirmInputBox,
filePathConfirmInputBoxMode: filePathConfirmInputBoxMode,
folderPathConfig: folderPathConfig,
basePathConfig: basePathConfig,
suffixConfig: suffixConfig,
forceUnixStyleSeparatorConfig: forceUnixStyleSeparatorConfig,
encodePathConfig: encodePathConfig,
namePrefixConfig: namePrefixConfig,
nameSuffixConfig: nameSuffixConfig,
insertPatternConfig: insertPatternConfig,
showFilePathConfirmInputBox: showFilePathConfirmInputBox,
filePathConfirmInputBoxMode: filePathConfirmInputBoxMode,
folderPathConfig: folderPathConfig,
basePathConfig: basePathConfig,

};

return config;

}

// Variables that can be used to replace values in config paths
const PATH_VARIABLE_CURRENT_FILE_DIR = /\$\{currentFileDir\}/g;
const PATH_VARIABLE_PROJECT_ROOT = /\$\{projectRoot\}/g;
const PATH_VARIABLE_CURRENT_FILE_NAME = /\$\{currentFileName\}/g;
const PATH_VARIABLE_CURRENT_FILE_NAME_WITHOUT_EXT = /\$\{currentFileNameWithoutExt\}/g;

export const replacePathVariable = (pathStr: string, projectRoot: string, curFilePath: string, postFunction: (val: string) => string = (x) => x): string => {
let currentFileDir = path.dirname(curFilePath);
let ext = path.extname(curFilePath);
let fileName = path.basename(curFilePath);
let fileNameWithoutExt = path.basename(curFilePath, ext);
export const replacePathVariable = ({ pathStr, projectRoot, editorOpenFilePath }: { pathStr: string; projectRoot: string; editorOpenFilePath: string }): string => {
let currentFileDir = path.dirname(editorOpenFilePath);
let ext = path.extname(editorOpenFilePath);
let fileName = path.basename(editorOpenFilePath);
let fileNameWithoutExt = path.basename(editorOpenFilePath, ext);

pathStr = pathStr.replace(PATH_VARIABLE_PROJECT_ROOT, postFunction(projectRoot));
pathStr = pathStr.replace(PATH_VARIABLE_CURRENT_FILE_DIR, postFunction(currentFileDir));
pathStr = pathStr.replace(PATH_VARIABLE_CURRENT_FILE_NAME, postFunction(fileName));
pathStr = pathStr.replace(PATH_VARIABLE_CURRENT_FILE_NAME_WITHOUT_EXT, postFunction(fileNameWithoutExt));
pathStr = pathStr.replace(PATH_VARIABLE_PROJECT_ROOT, projectRoot);
pathStr = pathStr.replace(PATH_VARIABLE_CURRENT_FILE_DIR, currentFileDir);
pathStr = pathStr.replace(PATH_VARIABLE_CURRENT_FILE_NAME, fileName);
pathStr = pathStr.replace(PATH_VARIABLE_CURRENT_FILE_NAME_WITHOUT_EXT, fileNameWithoutExt);
return pathStr;
}

2 changes: 1 addition & 1 deletion src/folderUtil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('FolderUtil', () => {
expect(await makeImagePath({
folderPathConfig: "docs/img",
fileName: 'notRealFile.png',
filePath: testPath
editorOpenFilePath: testPath
})).contain('/src/docs/img/notRealFile.png')
})

Expand Down
4 changes: 2 additions & 2 deletions src/folderUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export const createImageDirWithImagePath = async (imagePath: string): Promise<st
* create directory for image when directory does not exist
*/

export const makeImagePath = ({ folderPathConfig, fileName, filePath }: { folderPathConfig: string; fileName: string; filePath: string; }): string => {
export const makeImagePath = ({ folderPathConfig, fileName, editorOpenFilePath }: { folderPathConfig: string; fileName: string; editorOpenFilePath: string; }): string => {
// image output path
let folderPath = path.dirname(filePath);
let folderPath = path.dirname(editorOpenFilePath);
let imagePath = "";

// generate image path
Expand Down

0 comments on commit 92681c9

Please sign in to comment.