Skip to content

Commit

Permalink
XS✔ ◾ Message Filtering (#510)
Browse files Browse the repository at this point in the history
## Summary

This change primarily focuses on adding a new feature to the `Logger`
class in `src/task/src/utilities/logger.ts` to filter control strings
from the log messages. The changes also include updates to the
corresponding tests in `src/task/tests/utilities/logger.spec.ts` to
validate the new functionality.

Here are the key changes:

**Logger Class Enhancements:**

*
[`src/task/src/utilities/logger.ts`](diffhunk://#diff-5edc22bc177458bb92a473da7eb8dd56e9cdd5091f222c90830e9e7de380030cR33):
Added a new method `filterMessage` to the `Logger` class. This method
removes control strings from the log messages to prevent them from being
printed to `stdout`. This filtering is now applied in the `logDebug`,
`logInfo`, `logWarning`, and `logError` methods.
[[1]](diffhunk://#diff-5edc22bc177458bb92a473da7eb8dd56e9cdd5091f222c90830e9e7de380030cR33)
[[2]](diffhunk://#diff-5edc22bc177458bb92a473da7eb8dd56e9cdd5091f222c90830e9e7de380030cR43)
[[3]](diffhunk://#diff-5edc22bc177458bb92a473da7eb8dd56e9cdd5091f222c90830e9e7de380030cR53)
[[4]](diffhunk://#diff-5edc22bc177458bb92a473da7eb8dd56e9cdd5091f222c90830e9e7de380030cR63)
[[5]](diffhunk://#diff-5edc22bc177458bb92a473da7eb8dd56e9cdd5091f222c90830e9e7de380030cR88-R96)

**Test Updates:**

*
[`src/task/tests/utilities/logger.spec.ts`](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR21-R37):
Added new test cases for each of the logging methods (`logDebug`,
`logInfo`, `logWarning`, `logError`, `logErrorObject`, and `replay`) to
validate that control strings are correctly filtered out from the log
messages.
[[1]](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR21-R37)
[[2]](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR51-R67)
[[3]](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR81-R97)
[[4]](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR111-R127)
[[5]](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR141-R162)
[[6]](diffhunk://#diff-4deade9fa59bc7d857956add0b8fac29f34b9173c24fee51f440b4ee19336d3fR197-R236)

## Testing

### Test Types

- [X] Unit tests
- [X] Manual tests

### Unit Test Coverage

100%

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
muiriswoulfe and github-actions[bot] authored Jun 7, 2024
1 parent addd60a commit 9bb8463
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/task/src/utilities/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class Logger {
* @param message The message to log.
*/
public logDebug (message: string): void {
message = Logger.filterMessage(message)
this._messages.push(`debug – ${message}`)
this._runnerInvoker.logDebug(message)
}
Expand All @@ -39,6 +40,7 @@ export default class Logger {
* @param message The message to log.
*/
public logInfo (message: string): void {
message = Logger.filterMessage(message)
this._messages.push(`info – ${message}`)
this._consoleWrapper.log(message)
}
Expand All @@ -48,6 +50,7 @@ export default class Logger {
* @param message The message to log.
*/
public logWarning (message: string): void {
message = Logger.filterMessage(message)
this._messages.push(`warning – ${message}`)
this._runnerInvoker.logWarning(message)
}
Expand All @@ -57,6 +60,7 @@ export default class Logger {
* @param message The message to log.
*/
public logError (message: string): void {
message = Logger.filterMessage(message)
this._messages.push(`error – ${message}`)
this._runnerInvoker.logError(message)
}
Expand All @@ -81,4 +85,13 @@ export default class Logger {
this._consoleWrapper.log(`🔁 ${message}`)
})
}

/**
* Filter messages so that control strings are not printed to `stdout`.
* @param message The message to filter.
* @returns The filtered message.
*/
private static filterMessage (message: string): string {
return message.replace(/##(vso)?\[/gi, '')
}
}
130 changes: 130 additions & 0 deletions src/task/tests/utilities/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ describe('logger.ts', (): void => {
})

describe('logDebug()', (): void => {
[
'##[test]Message',
'##vso[test]Message',
'##VSO[test]Message'
].forEach((value: string): void => {
it(`should log the filtered message for '${value}'`, (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))

// Act
logger.logDebug(value)

// Assert
verify(runnerInvoker.logDebug('test]Message')).once()
})
})

it('should log the message', (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
Expand All @@ -31,6 +48,23 @@ describe('logger.ts', (): void => {
})

describe('logInfo()', (): void => {
[
'##[test]Message',
'##vso[test]Message',
'##VSO[test]Message'
].forEach((value: string): void => {
it(`should log the filtered message for '${value}'`, (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))

// Act
logger.logInfo(value)

// Assert
verify(consoleWrapper.log('test]Message')).once()
})
})

it('should log the message', (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
Expand All @@ -44,6 +78,23 @@ describe('logger.ts', (): void => {
})

describe('logWarning()', (): void => {
[
'##[test]Message',
'##vso[test]Message',
'##VSO[test]Message'
].forEach((value: string): void => {
it(`should log the filtered message for '${value}'`, (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))

// Act
logger.logWarning(value)

// Assert
verify(runnerInvoker.logWarning('test]Message')).once()
})
})

it('should log the message', (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
Expand All @@ -57,6 +108,23 @@ describe('logger.ts', (): void => {
})

describe('logError()', (): void => {
[
'##[test]Message',
'##vso[test]Message',
'##VSO[test]Message'
].forEach((value: string): void => {
it(`should log the filtered message for '${value}'`, (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))

// Act
logger.logError(value)

// Assert
verify(runnerInvoker.logError('test]Message')).once()
})
})

it('should log the message', (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
Expand All @@ -70,6 +138,28 @@ describe('logger.ts', (): void => {
})

describe('logErrorObject()', (): void => {
[
'##[test]',
'##vso[test]',
'##VSO[test]'
].forEach((value: string): void => {
it(`should log all filtered properties '${value}' of the error object`, (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
const error: Error = new Error(`${value}Message`)
error.name = `${value}Error`
error.stack = `${value}Stack contents`

// Act
logger.logErrorObject(error)

// Assert
verify(consoleWrapper.log('test]Error – name: "test]Error"')).once()
verify(consoleWrapper.log('test]Error – message: "test]Message"')).once()
verify(consoleWrapper.log('test]Error – stack: "test]Stack contents"')).once()
})
})

it('should log all properties of the error object', (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
Expand Down Expand Up @@ -104,6 +194,46 @@ describe('logger.ts', (): void => {
})

describe('replay()', (): void => {
[
'##[test]',
'##vso[test]',
'##VSO[test]'
].forEach((value: string): void => {
it(`should replay all filtered messages '${value}'`, (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
logger.logDebug(`${value}Debug Message 1`)
logger.logInfo(`${value}Info Message 1`)
logger.logWarning(`${value}Warning Message 1`)
logger.logError(`${value}Error Message 1`)
logger.logDebug(`${value}Debug Message 2`)
logger.logInfo(`${value}Info Message 2`)
logger.logWarning(`${value}Warning Message 2`)
logger.logError(`${value}Error Message 2`)

// Act
logger.replay()

// Assert
verify(runnerInvoker.logDebug('test]Debug Message 1')).once()
verify(consoleWrapper.log('test]Info Message 1')).once()
verify(runnerInvoker.logWarning('test]Warning Message 1')).once()
verify(runnerInvoker.logError('test]Error Message 1')).once()
verify(runnerInvoker.logDebug('test]Debug Message 2')).once()
verify(consoleWrapper.log('test]Info Message 2')).once()
verify(runnerInvoker.logWarning('test]Warning Message 2')).once()
verify(runnerInvoker.logError('test]Error Message 2')).once()
verify(consoleWrapper.log('🔁 debug – test]Debug Message 1')).once()
verify(consoleWrapper.log('🔁 info – test]Info Message 1')).once()
verify(consoleWrapper.log('🔁 warning – test]Warning Message 1')).once()
verify(consoleWrapper.log('🔁 error – test]Error Message 1')).once()
verify(consoleWrapper.log('🔁 debug – test]Debug Message 2')).once()
verify(consoleWrapper.log('🔁 info – test]Info Message 2')).once()
verify(consoleWrapper.log('🔁 warning – test]Warning Message 2')).once()
verify(consoleWrapper.log('🔁 error – test]Error Message 2')).once()
})
})

it('should replay all messages', (): void => {
// Arrange
const logger: Logger = new Logger(instance(consoleWrapper), instance(runnerInvoker))
Expand Down

0 comments on commit 9bb8463

Please sign in to comment.