Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions src/generator/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,40 @@ export async function downloadDiscoveryDocs(
return changes;
}

/**
* Checks that a discovery doc file name is in the expected format of
* [NAME(alphanumeric)]-[VERSION(alphanumeric, _, .)].json.
* Throws an error if the file name is not in the expected format.
* @param fileName The file name to validate
*/
export function validateDiscoveryDocFileName(fileName: string) {
const regex = /^[a-zA-Z0-9]+-[a-zA-Z0-9_.]+\.json$/;
if (!regex.test(fileName)) {
throw new Error(
`Discovery doc file name '${fileName}' is not in the expected format of '[NAME(alphanumeric)]-[VERSION(alphanumeric, _, .)].json'.`,
);
}
}

export interface ApiData {
name: string;
version: string;
}

/**
* Parses a discovery doc file name and returns the API name and version.
* @param fileName The file name to parse.
* @returns The API data (name and version).
*/
export function getApiData(fileName: string): ApiData {
validateDiscoveryDocFileName(fileName);
const firstHyphen = fileName.indexOf('-');
const lastDot = fileName.lastIndexOf('.');
const name = fileName.substring(0, firstHyphen);
const version = fileName.substring(firstHyphen + 1, lastDot);
return {name, version};
}

// These are libraries we should no longer support because
// they are not present in the index.json
// example: b/148605368
Expand All @@ -123,22 +157,21 @@ function cleanupLibrariesNotInIndexJSON(
const srcPath = path.join(__dirname, '../../../src', 'apis');
const discoveryDirectory = fs.readdirSync(options.downloadPath);
const apisReplaced = apis.map(
x => x.id.toString().replace(':', '-') + '.json',
api => api.id.toString().replace(':', '-') + '.json',
);
// So that we don't delete index.json
apisReplaced.push('index.json');
const discoveryDocsToDelete = discoveryDirectory.filter(
x => !apisReplaced.includes(x),
fileName => !apisReplaced.includes(fileName),
);
const clientFilesToDelete = discoveryDocsToDelete.map(x => {
const apiName = x.split('-')[0];
const versionName = apiName[1].split('.')[0];
return path.join(srcPath, apiName, `${versionName}.ts`);
const clientFilesToDelete = discoveryDocsToDelete.map(docFileName => {
const api = getApiData(docFileName);
return path.join(srcPath, api.name, `${api.version}.ts`);
});
discoveryDocsToDelete.forEach(x =>
fs.unlinkSync(path.join(options.downloadPath, x)),
discoveryDocsToDelete.forEach(docFileName =>
fs.unlinkSync(path.join(options.downloadPath, docFileName)),
);
clientFilesToDelete.forEach(x => fs.unlinkSync(x));
clientFilesToDelete.forEach(clientFile => fs.unlinkSync(clientFile));
}

const ignoreLines = /^\s+"(?:etag|revision)": ".+"/;
Expand Down
51 changes: 51 additions & 0 deletions test/test.download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@
scopes.forEach(s => s.done());
});

it('should clean up old files', async () => {
const readdirSync = sandbox.stub(fs, 'readdirSync');
const unlinkSync = sandbox.stub(fs, 'unlinkSync');
const downloadPath = 'build/test/temp';
readdirSync.returns(['a-v1.json', 'blogger-v2.json'] as any);

Check warning on line 160 in test/test.download.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 160 in test/test.download.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const scopes = [
nock(
'https://raw.githubusercontent.com/googleapis/discovery-artifact-manager/master/discoveries',
)
.get('/index.json')
.reply(200, JSON.stringify(fs.readFileSync(fakeIndexPath, 'utf8')), {
'Content-Type': 'application/json',
}),
nock(
'https://raw.githubusercontent.com/googleapis/discovery-artifact-manager/master/discoveries',
)
.get('/fake.v1.json')
.reply(
200,
'{"id": "fake:v1","discoveryRestUrl": "http://localhost:3030/path","name": "fake","version": "v1"}',
),
];
await dn.downloadDiscoveryDocs({discoveryUrl, downloadPath});
assert(unlinkSync.calledWith(path.join(downloadPath, 'blogger-v2.json')));
const expected = path.join(__dirname, '../../src/apis/blogger/v2.ts');
assert(unlinkSync.calledWith(expected));
scopes.forEach(s => s.done());
});

it('should be invokable from the CLI', async () => {
const port = 3030;
const server = http
Expand Down Expand Up @@ -235,4 +264,26 @@
const result = dn.getDiffs(oldDoc, newDoc);
assert.deepStrictEqual(result, expected);
});

it('should validate discovery doc file names', () => {
assert.doesNotThrow(() =>
dn.validateDiscoveryDocFileName('apiname-v1.json'),
);
assert.doesNotThrow(() =>
dn.validateDiscoveryDocFileName('apiname-v1.1.json'),
);
assert.doesNotThrow(() =>
dn.validateDiscoveryDocFileName('apiname-v1_beta.json'),
);
assert.throws(() =>
dn.validateDiscoveryDocFileName('apiname-v1-extra.json'),
);
assert.throws(() => dn.validateDiscoveryDocFileName('api_name-v1.json'));
assert.throws(() => dn.validateDiscoveryDocFileName('apiname-v1.js'));
});

it('should parse discovery doc file names', () => {
const result = dn.getApiData('apiname-v1.2.json');
assert.deepStrictEqual(result, {name: 'apiname', version: 'v1.2'});
});
});
Loading