diff --git a/tests/lib/chrome-source.js b/tests/lib/chrome-source.js new file mode 100644 index 0000000..2be0799 --- /dev/null +++ b/tests/lib/chrome-source.js @@ -0,0 +1,53 @@ +/** + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://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. + */ + +import test from 'ava'; +import {tmpdir} from 'os'; +import { urlFor, fetchAllTo } from '../../tools/lib/chrome-source.js'; + +const revision = '23a41c068e35f33df1c3579a3b0b469d4458e6c1'; +const chromePath = 'chrome/common/apps/platform_apps/api'; +const chromePaths = [ + 'tools/json_schema_compiler', + 'tools/json_schema_compiler/test', + 'tools/json_comment_eater', + 'ppapi/generators', + 'third_party/ply', +]; + +test('urlFor', (t) => { + // Returns valid url + const uf = urlFor(revision, chromePath); + t.is(uf.startsWith('https://'), true); + t.is(uf.includes(revision), true); + t.is(uf.endsWith(`${chromePath}.tar.gz`), true); +}); + +test('fetchAllTo', async (t) => { + let fta; + // Returns chrome source tree files for each path + fta = await fetchAllTo(tmpdir(), chromePaths, revision); + t.is(Array.isArray(fta), true); + t.is(fta.length, chromePaths.length); + + // Throws error with invalid arguments + // @ts-ignore + await t.throwsAsync(() => fetchAllTo(null, chromePaths, revision)); + // @ts-ignore + t.throws(() => fetchAllTo(tmpdir(), null, revision)); + // @ts-ignore + await t.throwsAsync(() => fetchAllTo(tmpdir(), chromePaths, null)); +}); diff --git a/tools/lib/chrome-source.js b/tools/lib/chrome-source.js index 290da47..75a7743 100644 --- a/tools/lib/chrome-source.js +++ b/tools/lib/chrome-source.js @@ -33,7 +33,7 @@ import fetch from 'node-fetch'; * @param {string} chromePath * @return {string} */ -function urlFor(revision, chromePath) { +export function urlFor(revision, chromePath) { return `https://chromium.googlesource.com/chromium/src/+archive/${revision}/${chromePath}.tar.gz`; } @@ -46,7 +46,7 @@ function urlFor(revision, chromePath) { * @param {string} revision revision to fetch * @return {Promise<(string[]?)[]>} files written for paths, null for skipped */ -export async function fetchAllTo(targetPath, chromePaths, revision) { +export function fetchAllTo(targetPath, chromePaths, revision) { // This removes any trailing "/" from the paths. chromePaths = chromePaths.map(chromePath => path.join(chromePath, '.'));