Skip to content

Commit

Permalink
Unit tests and fixed regex
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmittag committed Apr 19, 2024
1 parent f6425a7 commit 35e1c82
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 27 deletions.
31 changes: 4 additions & 27 deletions src/modules/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,15 @@ import { join, resolve } from 'node:path';
import { ExecSpec, ProgramSpec } from './component';
import { ProjectCache } from './project-cache';
import { ProjectConfig } from './project-config';

const CACHE_OUTPUT_REGEX: RegExp =
/(\w+)\s*=\s*(\[((\'.*?\',\s*|\".*?\",\s*|\w+,\s*|\'.*?\'(?=\])|\".*?\"(?=\])|\w+(?=\]))*\])|(\'.*?\'|\".*?\"|\w+))\s+\>\>\s+VELOCITAS_CACHE/;

function stdOutParser(projectCache: ProjectCache, line: string) {
const match = CACHE_OUTPUT_REGEX.exec(line);
if (match && match.length > 0) {
const [_ignored, key, value] = match;
const cleanedValue = value.replace(/['"]/g, '');
if (cleanedValue.startsWith('[')) {
const arrayPart = cleanedValue.substring(1, cleanedValue.length - 1);
const array = arrayPart.split(',');
const trimmedArray = array.map((str) => str.trim());
projectCache.set(key, trimmedArray);
} else {
projectCache.set(key, cleanedValue);
}
}
}
import { stdOutParser } from './stdout-parser';

const lineCapturer = (projectCache: ProjectCache, writeStdout: boolean, data: string) => {
if (writeStdout) {
process.stdout.write(data);
}
for (let line of data.toString().split('\n')) {
let lineTrimmed = (line as string).trim();

if (lineTrimmed.length === 0) {
continue;
}
stdOutParser(projectCache, lineTrimmed);
}
data.toString()
.split('\n')
.forEach((value) => stdOutParser(projectCache, value));
};

export function setSpawnImplementation(func: (command: string, args: string | string[], options: any) => IPty) {
Expand Down
42 changes: 42 additions & 0 deletions src/modules/stdout-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// This program and the accompanying materials are made available under the
// terms of the Apache License, Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// SPDX-License-Identifier: Apache-2.0

import { info } from 'console';
import { ProjectCache } from './project-cache';

const CACHE_OUTPUT_REGEX: RegExp =
/(\w+)\s*=\s*(\[((\'(\/*\w+)*\',\s*|(\"(\/*\w+)*\",\s*)|(\/*\w+)*,\s*|\'(\/*\w+)*\'(?=\])|\"(\/*\w+)*\"(?=\])|(\/*\w+)*(?=\]))*\])|(\'.*?\'|\".*?\"|\w+))\s+\>\>\s+VELOCITAS_CACHE/;

export function stdOutParser(projectCache: ProjectCache, line: string) {
let lineTrimmed = (line as string).trim();

if (lineTrimmed.length === 0) {
return;
}
const match = CACHE_OUTPUT_REGEX.exec(line);
if (match && match.length > 0) {
const [_ignored, key, value] = match;
const cleanedValue = value.replace(/['"]/g, '');
info(cleanedValue);
info(key);
if (cleanedValue.startsWith('[') && cleanedValue.endsWith(']')) {
const arrayPart = cleanedValue.substring(1, cleanedValue.length - 1);
const array = arrayPart.split(',');
const trimmedArray = array.map((str) => str.trim());
projectCache.set(key, trimmedArray);
} else {
projectCache.set(key, cleanedValue);
}
}
}
69 changes: 69 additions & 0 deletions test/unit/stdout-parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// This program and the accompanying materials are made available under the
// terms of the Apache License, Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// SPDX-License-Identifier: Apache-2.0

import { expect } from 'chai';
import { ProjectCache } from '../../src/modules/project-cache';
import { stdOutParser } from '../../src/modules/stdout-parser';
import { CliFileSystem, MockFileSystem, MockFileSystemObj } from '../../src/utils/fs-bridge';
import { getCacheData } from '../helpers/cache';

describe('stdOutParser - module', () => {
var matches = new Map<string, any>([
['test_1 = "/this/is/path/one" >> VELOCITAS_CACHE', '/this/is/path/one'],
["test_2 = '/this/is/path/one' >> VELOCITAS_CACHE", '/this/is/path/one'],
['test_3 = test3 >> VELOCITAS_CACHE', 'test3'],
['test_4 = ["/this/is/path/one", "/this/is/path/two"] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
['test_5 = ["/this/is/path/one","/this/is/path/two"] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
['test_6 = ["/this/is/path/one", "/this/is/path/two"] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
['test_7 =["/this/is/path/one", "/this/is/path/two"] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
['test_8=["/this/is/path/one", "/this/is/path/two"] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
["test_9 = ['/this/is/path/one', /this/is/path/two] >> VELOCITAS_CACHE", ['/this/is/path/one', '/this/is/path/two']],
['test_10 = [/this/is/path/one, "/this/is/path/two"] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
['test_11 = [/this/is/path/one, /this/is/path/two] >> VELOCITAS_CACHE', ['/this/is/path/one', '/this/is/path/two']],
]);

before(() => {
const projectCacheDir = ProjectCache.getCacheDir();
const mockFilesystem: MockFileSystemObj = {
[`${projectCacheDir}/cache.json`]: '{}',
};
CliFileSystem.setImpl(new MockFileSystem(mockFilesystem));
});
for (let [element, result] of matches) {
it('should match and set project cache correct', () => {
let id = element.split('=')[0].trim();
const projectCache = ProjectCache.read();
stdOutParser(projectCache, element);
expect(JSON.stringify(projectCache.get(id))).to.equal(JSON.stringify(result));
});
}

var noMatches = new Map<string, any>([
['test_1 = "/this/is/path/one >> VELOCITAS_CACHE', {}],
["test_2 = '/this/is/path/one >> VELOCITAS_CACHE", {}],
['test_3 = /this/is/path/one"this/test >> VELOCITAS_CACHE', {}],
['test_4 = ["/this/is/path/one", /this/is/path/two"]', {}],
['test_5 = ["/this/is/path/one""/this/is/path/two"] >> VELOCITAS_CACHE', {}],
['"test_6" = ["/this/is/path/one", "/this/is/path/two"] >> VELOCITAS_CACHE', {}],
['test_7 = [/this/is/path/one""/this/is/path/two"] >> VELOCITAS_CACHE', {}],
["test_8 = ['/this/is/path/one''/this/is/path/two'] >> VELOCITAS_CACHE", {}],
]);
noMatches.forEach((result, element) => {
it('should not match for wrong inputs', async () => {
const projectCache = ProjectCache.read();
stdOutParser(projectCache, element);
expect(JSON.stringify(getCacheData())).to.equal(JSON.stringify(result));
});
});
});

0 comments on commit 35e1c82

Please sign in to comment.