Skip to content

Commit

Permalink
build: fix mysterious await failure (#2117)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored May 19, 2020
1 parent c12710a commit 261d4d7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@types/node": "^12.0.1",
"@types/nunjucks": "^3.1.1",
"@types/prettier": "^2.0.0",
"@types/proxyquire": "^1.3.28",
"@types/qs": "^6.5.3",
"@types/rimraf": "^3.0.0",
"@types/sinon": "^9.0.0",
Expand All @@ -80,6 +81,7 @@
"open": "^7.0.0",
"p-queue": "^6.0.0",
"prettier": "^2.0.5",
"proxyquire": "^2.1.3",
"rimraf": "^3.0.0",
"server-destroy": "^1.0.1",
"sinon": "^9.0.2",
Expand Down
8 changes: 6 additions & 2 deletions src/generator/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export async function downloadDiscoveryDocs(options: DownloadOptions) {
gfs.writeFile(indexPath, res.data);
const queue = new Q({concurrency: 25});
console.log(`Downloading ${apis.length} APIs...`);
const changes = await queue.addAll(
// If you await the result of `queue.addAll`, the process mysteriously
// exits after downloading all the files. I have no earthly idea why.
// Software is terrible.
//const changes = await queue.addAll(
queue.addAll(
apis.map(api => {
return async () => {
console.log(`Downloading ${api.id}...`);
Expand Down Expand Up @@ -90,7 +94,7 @@ export async function downloadDiscoveryDocs(options: DownloadOptions) {
})
);
await queue.onIdle();
return changes;
//return changes;
}

const ignoreLines = /^\s+"(?:etag|revision)": ".+"/;
Expand Down
56 changes: 56 additions & 0 deletions test/test.generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 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 * as assert from 'assert';
import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';
import {describe, it, afterEach} from 'mocha';
import * as nock from 'nock';
import * as gen from '../src/generator/generator';

describe(__filename, () => {
nock.disableNetConnect();
const sandbox = sinon.createSandbox();

afterEach(() => sandbox.restore());

it('should read from cache if flag is passed', async () => {
// This test relies on the disabled network being enabled
const generator = new gen.Generator();
const genStub = sandbox.stub(generator, 'generateAPI').resolves();
const idxStub = sandbox.stub(generator, 'generateIndex').resolves();
await generator.generateAllAPIs('', true);
assert.ok(genStub.called);
assert.ok(idxStub.calledOnce);
});

it('should read from disco if flag is passed', async () => {
let downloadCalled = false;
const {Generator} = proxyquire('../src/generator/generator', {
'./download': {
downloadDiscoveryDocs: async () => {
downloadCalled = true;
},
},
});
const generator = new Generator();
const genStub = sandbox.stub(generator, 'generateAPI').resolves();
const idxStub = sandbox.stub(generator, 'generateIndex').resolves();
const discoUrl = 'https://www.googleapis.com/discovery/v1/apis/';
await generator.generateAllAPIs(discoUrl, false);
assert.ok(genStub.called);
assert.ok(idxStub.calledOnce);
assert.ok(downloadCalled);
});
});

0 comments on commit 261d4d7

Please sign in to comment.