Skip to content

Commit

Permalink
Can't use the parser in a web environment part 2 #67 (#68)
Browse files Browse the repository at this point in the history
* Remove reference to process
* added and external loggingEnabled method to switch on logging
* added logging test
* bumped version to 2.0.1

Signed-off-by: Wim Jongman <[email protected]>
  • Loading branch information
wimjongman authored Feb 10, 2025
1 parent 9a332d3 commit 28a3569
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ibm/ibmi-eventf-parser",
"description": "Parse event files that are generated by IBM i compilers",
"version": "2.0.0",
"version": "2.0.1",
"publisher": "IBM",
"author": "IBM",
"license": "Apache-2.0",
Expand Down
14 changes: 11 additions & 3 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ class LookAheadReader implements ISequentialFileReader {
}

export class Parser {
static LOGGER: any;

private _loggingEnabled: boolean = false;
private exception: Error | undefined;
private processor: IProcessor | undefined;
private lastOutputFile: SourceFile | undefined;
Expand All @@ -82,8 +81,17 @@ export class Parser {
return message;
}

/**
* Enable or disable logging.
*
* @param {boolean} enable true to enable or false to disable logging
*/
loggingEnabled(enable: boolean) {
this._loggingEnabled = enable;
}

private log(content: string) {
if (process && process.env.DEBUG) {
if (this._loggingEnabled) {
console.log(content);
}
}
Expand Down
18 changes: 17 additions & 1 deletion vitest/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* (c) Copyright IBM Corp. 2023
*/

import { assert, describe, it } from 'vitest'
import { assert, describe, expect, it, vi } from 'vitest'
import { Parser } from '../src/Parser';
import { FileIDRecord } from '../src/record/FileIDRecord';
import { MarkerCreator } from './MarkerCreator';
Expand Down Expand Up @@ -243,6 +243,22 @@ describe('Tests', () => {
const exceptions = parser.getException();
assert.strictEqual(exceptions, undefined);
});

it('test Logging', () => {
const consoleSpy = vi.spyOn(console, 'log');
const parser = new Parser();
const markerCreator = new MarkerCreator();

// No logging
parser.loggingEnabled(false);
parser.parse(new FileReader(`${TEST_DIR}/TYPICAL.PGM.evfevent`), markerCreator);
expect(consoleSpy).toHaveBeenCalledTimes(0);

// Logging
parser.loggingEnabled(true);
parser.parse(new FileReader(`${TEST_DIR}/TYPICAL.PGM.evfevent`), markerCreator);
expect(consoleSpy).toHaveBeenCalled();
});
});

function assertFileIDRecord(record: FileIDRecord, fileName: string, fileID: number, flag: number) {
Expand Down

0 comments on commit 28a3569

Please sign in to comment.