-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Logpoints (#343)
* feat: initial dev of logpoint. (#258) feat: adding types for string-replace-async library feat: linting and prettifying feat: adding tests for logpoint class feat: fixing formatting feat: fixing doc for attribute * Implemented logpoint support. Co-authored-by: Augusto César Dias <[email protected]> Co-authored-by: Damjan Cvetko <[email protected]>
- Loading branch information
1 parent
9a73045
commit cb091b7
Showing
7 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import stringReplaceAsync = require('string-replace-async') | ||
import { isWindowsUri } from './paths' | ||
|
||
export class LogPointManager { | ||
private _logpoints = new Map<string, Map<number, string>>() | ||
|
||
public addLogPoint(fileUri: string, lineNumber: number, logMessage: string) { | ||
if (isWindowsUri(fileUri)) { | ||
fileUri = fileUri.toLowerCase() | ||
} | ||
if (!this._logpoints.has(fileUri)) { | ||
this._logpoints.set(fileUri, new Map<number, string>()) | ||
} | ||
this._logpoints.get(fileUri)!.set(lineNumber, logMessage) | ||
} | ||
|
||
public clearFromFile(fileUri: string) { | ||
if (isWindowsUri(fileUri)) { | ||
fileUri = fileUri.toLowerCase() | ||
} | ||
if (this._logpoints.has(fileUri)) { | ||
this._logpoints.get(fileUri)!.clear() | ||
} | ||
} | ||
|
||
public hasLogPoint(fileUri: string, lineNumber: number): boolean { | ||
if (isWindowsUri(fileUri)) { | ||
fileUri = fileUri.toLowerCase() | ||
} | ||
return this._logpoints.has(fileUri) && this._logpoints.get(fileUri)!.has(lineNumber) | ||
} | ||
|
||
public async resolveExpressions( | ||
fileUri: string, | ||
lineNumber: number, | ||
callback: (expr: string) => Promise<string> | ||
): Promise<string> { | ||
if (isWindowsUri(fileUri)) { | ||
fileUri = fileUri.toLowerCase() | ||
} | ||
if (!this.hasLogPoint(fileUri, lineNumber)) { | ||
return Promise.reject('Logpoint not found') | ||
} | ||
const expressionRegex = /\{(.*?)\}/gm | ||
return await stringReplaceAsync( | ||
this._logpoints.get(fileUri)!.get(lineNumber)!, | ||
expressionRegex, | ||
function (_: string, group: string) { | ||
return group.length === 0 ? Promise.resolve('') : callback(group) | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { LogPointManager } from '../logpoint' | ||
import * as assert from 'assert' | ||
|
||
describe('logpoint', () => { | ||
const FILE_URI1 = 'file://my/file1' | ||
const FILE_URI2 = 'file://my/file2' | ||
const FILE_URI3 = 'file://my/file3' | ||
|
||
const LOG_MESSAGE_VAR = '{$variable1}' | ||
const LOG_MESSAGE_MULTIPLE = '{$variable1} {$variable3} {$variable2}' | ||
const LOG_MESSAGE_TEXT_AND_VAR = 'This is my {$variable1}' | ||
const LOG_MESSAGE_TEXT_AND_MULTIVAR = 'Those variables: {$variable1} ${$variable2} should be replaced' | ||
const LOG_MESSAGE_REPEATED_VAR = 'This {$variable1} and {$variable1} should be equal' | ||
const LOG_MESSAGE_BADLY_FORMATED_VAR = 'Only {$variable1} should be resolved and not }$variable1 and $variable1{}' | ||
|
||
const REPLACE_FUNCTION = (str: string): Promise<string> => { | ||
return Promise.resolve(`${str}_value`) | ||
} | ||
|
||
let logPointManager: LogPointManager | ||
|
||
beforeEach('create new instance', () => (logPointManager = new LogPointManager())) | ||
|
||
describe('basic map management', () => { | ||
it('should contain added logpoints', () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_VAR) | ||
logPointManager.addLogPoint(FILE_URI1, 11, LOG_MESSAGE_VAR) | ||
logPointManager.addLogPoint(FILE_URI2, 12, LOG_MESSAGE_VAR) | ||
logPointManager.addLogPoint(FILE_URI3, 13, LOG_MESSAGE_VAR) | ||
|
||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 10), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 11), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI2, 12), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI3, 13), true) | ||
|
||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 12), false) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI2, 13), false) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI3, 10), false) | ||
}) | ||
|
||
it('should add and clear entries', () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_VAR) | ||
logPointManager.addLogPoint(FILE_URI1, 11, LOG_MESSAGE_VAR) | ||
logPointManager.addLogPoint(FILE_URI2, 12, LOG_MESSAGE_VAR) | ||
logPointManager.addLogPoint(FILE_URI3, 13, LOG_MESSAGE_VAR) | ||
|
||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 10), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 11), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI2, 12), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI3, 13), true) | ||
|
||
logPointManager.clearFromFile(FILE_URI1) | ||
|
||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 10), false) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI1, 11), false) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI2, 12), true) | ||
assert.equal(logPointManager.hasLogPoint(FILE_URI3, 13), true) | ||
}) | ||
}) | ||
|
||
describe('variable resolution', () => { | ||
it('should resolve variables', async () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_VAR) | ||
const result = await logPointManager.resolveExpressions(FILE_URI1, 10, REPLACE_FUNCTION) | ||
assert.equal(result, '$variable1_value') | ||
}) | ||
|
||
it('should resolve multiple variables', async () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_MULTIPLE) | ||
const result = await logPointManager.resolveExpressions(FILE_URI1, 10, REPLACE_FUNCTION) | ||
assert.equal(result, '$variable1_value $variable3_value $variable2_value') | ||
}) | ||
|
||
it('should resolve variables with text', async () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_TEXT_AND_VAR) | ||
const result = await logPointManager.resolveExpressions(FILE_URI1, 10, REPLACE_FUNCTION) | ||
assert.equal(result, 'This is my $variable1_value') | ||
}) | ||
|
||
it('should resolve multiple variables with text', async () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_TEXT_AND_MULTIVAR) | ||
const result = await logPointManager.resolveExpressions(FILE_URI1, 10, REPLACE_FUNCTION) | ||
assert.equal(result, 'Those variables: $variable1_value $$variable2_value should be replaced') | ||
}) | ||
|
||
it('should resolve repeated variables', async () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_REPEATED_VAR) | ||
const result = await logPointManager.resolveExpressions(FILE_URI1, 10, REPLACE_FUNCTION) | ||
assert.equal(result, 'This $variable1_value and $variable1_value should be equal') | ||
}) | ||
|
||
it('should resolve repeated bad formated messages correctly', async () => { | ||
logPointManager.addLogPoint(FILE_URI1, 10, LOG_MESSAGE_BADLY_FORMATED_VAR) | ||
const result = await logPointManager.resolveExpressions(FILE_URI1, 10, REPLACE_FUNCTION) | ||
assert.equal(result, 'Only $variable1_value should be resolved and not }$variable1 and $variable1') | ||
}) | ||
}) | ||
}) |