-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6425a7
commit 35e1c82
Showing
3 changed files
with
115 additions
and
27 deletions.
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
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,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); | ||
} | ||
} | ||
} |
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,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)); | ||
}); | ||
}); | ||
}); |