Skip to content

Commit

Permalink
fix(responses): response xml and json files (#247)
Browse files Browse the repository at this point in the history
- on clean, remove responses
- on update, if the size is 0 then get a new response
  • Loading branch information
cnishina authored May 5, 2017
1 parent cb3b70f commit 708ade3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
14 changes: 10 additions & 4 deletions lib/binaries/config_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ export abstract class XmlConfigSource extends ConfigSource {
try {
let contents = fs.readFileSync(fileName).toString();
let timestamp = new Date(fs.statSync(fileName).mtime).getTime();
let size = fs.statSync(fileName).size;
let now = Date.now();

// Ignore validating the cache OR if we are validating the cache, make
// it is within the cache time.
// On start, read the file. If not on start, check use the cache as long as the
// size > 0 and within the cache time.
// 60 minutes * 60 seconds / minute * 1000 ms / second
if (Config.runCommand === 'start' || (now - (60 * 60 * 1000) < timestamp)) {
if (Config.runCommand === 'start' || (size > 0 && (now - (60 * 60 * 1000) < timestamp))) {
return this.convertXml2js(contents);
} else {
return null;
Expand Down Expand Up @@ -169,8 +170,13 @@ export abstract class GithubApiConfigSource extends JsonConfigSource {
try {
let contents = fs.readFileSync(fileName).toString();
let timestamp = new Date(fs.statSync(fileName).mtime).getTime();
let size = fs.statSync(fileName).size;
let now = Date.now();
if (Config.runCommand === 'start' || (now - (60 * 60 * 1000) < timestamp)) {

// On start, read the file. If not on start, check use the cache as long as the
// size > 0 and within the cache time.
// 60 minutes * 60 seconds / minute * 1000 ms / second
if (Config.runCommand === 'start' || (size > 0 && (now - (60 * 60 * 1000) < timestamp))) {
return contents;
} else {
return null;
Expand Down
19 changes: 13 additions & 6 deletions lib/files/file_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,19 @@ export class FileManager {
}
});

let updateConfig = path.resolve(outputDir, 'update-config.json');
try {
fs.unlinkSync(updateConfig);
logger.info('removed update-config.json');
} catch (e) {
return;
let metaFiles = [
'chromedriver-response.xml', 'gecko-response.json', 'iedriver-response.xml',
'standalone-response.xml', 'update-config.json'
];
for (let metaFile of metaFiles) {
try {
let metaFilePath = path.resolve(outputDir, metaFile);
if (fs.statSync(metaFilePath)) {
fs.unlinkSync(metaFilePath);
logger.info('removed ' + metaFile);
}
} catch (e) {
}
}
}
}
35 changes: 34 additions & 1 deletion spec/binaries/config_source_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ describe('config', () => {
});

});

it('on update: if the size of the file is zero, invalidate the cache', done => {
spyOn(fs, 'statSync').and.callFake(() => {
return {size: 0};
});
Config.runCommand = 'update';
let xmlConfig = new XMLConfig('json', 'url');
xmlConfig.testGetXml()
.then(xml => {
// should do nothing
done.fail('this should not work');
})
.catch(err => {
expect(err.toString()).toContain('Invalid URI "url"');
done();
});
});
});

describe('github json', () => {
Expand Down Expand Up @@ -131,7 +148,23 @@ describe('config', () => {
expect(err.toString()).toContain('Invalid URI "url"');
done();
});

});

it('on update: if the size of the file is zero, invalidate the cache', done => {
spyOn(fs, 'statSync').and.callFake(() => {
return {size: 0};
});
Config.runCommand = 'update';
let jsonConfig = new JSONConfig('json', 'url');
jsonConfig.testGetJson()
.then(json => {
// should do nothing
done.fail('this should not work');
})
.catch(err => {
expect(err.toString()).toContain('Invalid URI "url"');
done();
});
});
});
});

0 comments on commit 708ade3

Please sign in to comment.