Skip to content

Commit

Permalink
Fix #4: Failure to load the CMake cache when using Windows newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
vector-of-bool committed Apr 21, 2016
1 parent 772258e commit e9bbf50
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
27 changes: 15 additions & 12 deletions src/cmake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,14 @@ export class CacheReader {
return await async.exists(this.path);
}

private _reloadData = async function (): Promise<Map<string, CacheEntry>> {
const _this: CacheReader = this;
console.info('Reloading CMake cache data from', _this.path);
const newdata = new Map<string, CacheEntry>();
const buf = await async.readFile(_this.path);
const contents = buf.toString();
const all_lines = contents.split('\n');
const filtered_lines = all_lines
public static parseCache(content: string): Map<string, CacheEntry> {
const lines = content.split(/\r\n|\n|\r/)
.filter(line => !!line.length)
.filter(line => !/^\s*#/.test(line));

const entries = new Map<string, CacheEntry>();
let docs_acc = '';
for (const line of filtered_lines) {
for (const line of lines) {
if (line.startsWith('//')) {
docs_acc += /^\/\/(.*)/.exec(line)[1] + ' ';
} else {
Expand All @@ -121,13 +116,21 @@ export class CacheReader {
value = isTruthy(value);

console.assert(type !== undefined, `Unknown cache entry type: ${type}`);
newdata.set(name, new CacheEntry(key, value, type, docs));
entries.set(name, new CacheEntry(key, value, type, docs));
}
}
}

return entries;
}

private _reloadData = async function (): Promise<Map<string, CacheEntry>> {
const _this: CacheReader = this;
console.info('Reloading CMake cache data from', _this.path);
const buf = await async.readFile(_this.path);
_this.data = CacheReader.parseCache(buf.toString());
_this._lastModifiedTime = (await async.stat(_this.path)).mtime;
return _this.data = newdata;
return _this.data;
}

public needsReloading = async function (): Promise<boolean> {
Expand Down Expand Up @@ -729,7 +732,7 @@ export class CMakeTools {
this._killTree(child.pid);
}

public _killTree = async function(pid: number) {
public _killTree = async function (pid: number) {
let children: number[] = [];
if (process.platform !== 'win32') {
const stdout = (await async.execute('pgrep', ['-P', pid.toString()])).stdout.trim();
Expand Down
24 changes: 22 additions & 2 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import * as cmake_tools_ext from '../src/extension';

import * as cmake from '../src/cmake';

const here = __dirname;

function testFilePath(filename: string): string {
return path.join(process.cwd(), 'test', filename);
return path.normalize(path.join(here, '../..', 'test', filename));
}


suite("Utility tests", () => {
test("Reloads only when needed", async function () {
const reader = new cmake.CacheReader(testFilePath('TestCMakeCache.txt'));
Expand Down Expand Up @@ -51,6 +53,24 @@ suite("Utility tests", () => {
true
);
});
test("Read cache with various newlines", async function() {
for (const newline of ['\n', '\r\n', '\r']) {
const str = [
'# This line is ignored',
'// This line is docs',
'SOMETHING:STRING=foo',
''
].join(newline);
const entries = cmake.CacheReader.parseCache(str);
const message = `Using newline ${JSON.stringify(newline)}`
assert.strictEqual(entries.size, 1, message);
assert.strictEqual(entries.has('SOMETHING'), true);
const entry = entries.get('SOMETHING');
assert.strictEqual(entry.value, 'foo');
assert.strictEqual(entry.type, cmake.EntryType.String);
assert.strictEqual(entry.docs, 'This line is docs');
}
});
test('Falsey values', () => {
for (const thing of [
'0',
Expand Down Expand Up @@ -80,5 +100,5 @@ suite("Utility tests", () => {
]) {
assert.strictEqual(cmake.isTruthy(thing), true, 'Testing truthiness of ' + thing);
}
})
});
});

0 comments on commit e9bbf50

Please sign in to comment.