Skip to content

Commit 877e953

Browse files
committed
added tests
1 parent 796656c commit 877e953

2 files changed

Lines changed: 141 additions & 14 deletions

File tree

packages/apollo-language-server/src/config/__tests__/loadConfig.ts

Lines changed: 133 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { loadConfig } from "../";
22
import * as path from "path";
33
import * as fs from "fs";
4+
import {
5+
DefaultClientConfig,
6+
DefaultServiceConfig,
7+
DefaultEngineConfig
8+
} from "../config";
49

510
const makeNestedDir = dir => {
611
if (fs.existsSync(dir)) return;
@@ -269,7 +274,7 @@ Object {
269274
});
270275

271276
describe("project type", () => {
272-
it("uses passed in type as override", async () => {
277+
it("uses passed in type when config doesnt have client/service", async () => {
273278
writeFilesToDir(dir, {
274279
"my.config.js": `module.exports = { engine: { endpoint: 'http://a.a' } }`
275280
});
@@ -282,21 +287,138 @@ Object {
282287

283288
expect(config.isClient).toEqual(true);
284289
});
285-
it("infers client projects", () => {});
286-
it("infers service projects", () => {});
287-
it("throws if project type cant be inferred", () => {});
290+
291+
it("infers client projects from config", async () => {
292+
writeFilesToDir(dir, {
293+
"my.config.js": `module.exports = { client: { service: 'hello' } }`
294+
});
295+
296+
const config = await loadConfig({
297+
configPath: dirPath,
298+
configFileName: "my.config.js"
299+
});
300+
301+
expect(config.isClient).toEqual(true);
302+
});
303+
304+
it("infers service projects from config", async () => {
305+
writeFilesToDir(dir, {
306+
"my.config.js": `module.exports = { service: 'wow' }`
307+
});
308+
309+
const config = await loadConfig({
310+
configPath: dirPath,
311+
configFileName: "my.config.js"
312+
});
313+
314+
expect(config.isService).toEqual(true);
315+
});
316+
317+
it("throws if project type cant be inferred", done => {
318+
writeFilesToDir(dir, {
319+
"my.config.js": `module.exports = { engine: { endpoint: 'http://a.a' } }`
320+
});
321+
322+
return loadConfig({
323+
configPath: dirPath,
324+
configFileName: "my.config.js"
325+
}).catch(err => {
326+
expect(err.message).toMatch(/.*Unable to resolve project type.*/);
327+
done();
328+
});
329+
});
288330
});
289331

290332
describe("service name", () => {
291-
it("lets config service name take precedence for client project", () => {});
292-
it("lets name passed in take precedence over env var", () => {});
293-
it("uses env var to determine service name when no other options", () => {});
333+
it("lets config service name take precedence for client project", async () => {
334+
writeFilesToDir(dir, {
335+
"my.config.js": `module.exports = { client: { service: 'hello' } }`,
336+
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
337+
});
338+
339+
const config = await loadConfig({
340+
configPath: dirPath,
341+
configFileName: "my.config.js",
342+
name: "not-it"
343+
});
344+
345+
expect(config.client.service).toEqual("hello");
346+
});
347+
348+
it("lets name passed in take precedence over env var", async () => {
349+
writeFilesToDir(dir, {
350+
"my.config.js": `module.exports = { client: { } }`,
351+
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
352+
});
353+
354+
const config = await loadConfig({
355+
configPath: dirPath,
356+
configFileName: "my.config.js",
357+
name: "hello"
358+
});
359+
360+
expect(config.client.service).toEqual("hello");
361+
});
362+
363+
it("uses env var to determine service name when no other options", async () => {
364+
writeFilesToDir(dir, {
365+
"my.config.js": `module.exports = { client: { } }`,
366+
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
367+
});
368+
369+
const config = await loadConfig({
370+
configPath: dirPath,
371+
configFileName: "my.config.js"
372+
});
373+
374+
expect(config.client.service).toEqual("harambe");
375+
});
294376
});
295377

296378
describe("default merging", () => {
297-
it("merges service name and default config for client projects", () => {});
298-
it("merges service name and default config for service projects", () => {});
299-
it("merges engine config with projects", () => {});
300-
it("merges defaults in at the end", () => {});
379+
it("merges service name and default config for client projects", async () => {
380+
writeFilesToDir(dir, {
381+
"my.config.js": `module.exports = { client: { service: 'hello' } }`
382+
});
383+
384+
const config = await loadConfig({
385+
configPath: dirPath,
386+
configFileName: "my.config.js"
387+
});
388+
389+
expect(config.rawConfig.client.includes).toEqual(
390+
DefaultClientConfig.includes
391+
);
392+
});
393+
394+
it("merges service name and default config for service projects", async () => {
395+
writeFilesToDir(dir, {
396+
"my.config.js": `module.exports = { service: { name: 'wow' } }`
397+
});
398+
399+
const config = await loadConfig({
400+
configPath: dirPath,
401+
configFileName: "my.config.js"
402+
});
403+
404+
expect(config.rawConfig.service.includes).toEqual(
405+
DefaultServiceConfig.includes
406+
);
407+
});
408+
409+
it("merges engine config defaults", async () => {
410+
writeFilesToDir(dir, {
411+
"my.config.js": `module.exports = { client: { service: 'wow' } }`
412+
});
413+
414+
const config = await loadConfig({
415+
configPath: dirPath,
416+
configFileName: "my.config.js"
417+
});
418+
419+
expect(config.rawConfig.engine.endpoint).toEqual(
420+
DefaultEngineConfig.endpoint
421+
);
422+
});
301423
});
302424
});

packages/apollo-language-server/src/config/loadConfig.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import cosmiconfig from "cosmiconfig";
22
import { LoaderEntry } from "cosmiconfig";
33
import TypeScriptLoader from "@endemolshinegroup/cosmiconfig-typescript-loader";
44
import { resolve } from "path";
5-
import { readFileSync, existsSync } from "fs";
5+
import { readFileSync, existsSync, lstatSync } from "fs";
66
import { merge, get } from "lodash/fp";
77
import {
88
ApolloConfig,
@@ -116,7 +116,7 @@ export async function loadConfig({
116116
? resolve(configPath, ".env")
117117
: resolve(process.cwd(), ".env");
118118

119-
if (existsSync(dotEnvPath)) {
119+
if (existsSync(dotEnvPath) && lstatSync(dotEnvPath).isFile()) {
120120
const env: { [key: string]: string } = require("dotenv").parse(
121121
readFileSync(dotEnvPath)
122122
);
@@ -153,7 +153,12 @@ export async function loadConfig({
153153

154154
// if there wasn't a config loaded from a file, build one.
155155
// if there was a service name found in the env, merge it with the new/existing config object.
156-
if (!loadedConfig || serviceName) {
156+
// if the config loaded doesn't have a client/service key, add one based on projectType
157+
if (
158+
!loadedConfig ||
159+
serviceName ||
160+
!(loadedConfig.config.client || loadedConfig.config.service)
161+
) {
157162
loadedConfig = {
158163
filepath: configPath || process.cwd(),
159164
config: {

0 commit comments

Comments
 (0)