Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeDawkins committed Mar 26, 2019
1 parent 796656c commit 877e953
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 14 deletions.
144 changes: 133 additions & 11 deletions packages/apollo-language-server/src/config/__tests__/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { loadConfig } from "../";
import * as path from "path";
import * as fs from "fs";
import {
DefaultClientConfig,
DefaultServiceConfig,
DefaultEngineConfig
} from "../config";

const makeNestedDir = dir => {
if (fs.existsSync(dir)) return;
Expand Down Expand Up @@ -269,7 +274,7 @@ Object {
});

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

expect(config.isClient).toEqual(true);
});
it("infers client projects", () => {});
it("infers service projects", () => {});
it("throws if project type cant be inferred", () => {});

it("infers client projects from config", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { service: 'hello' } }`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.isClient).toEqual(true);
});

it("infers service projects from config", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { service: 'wow' }`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.isService).toEqual(true);
});

it("throws if project type cant be inferred", done => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { engine: { endpoint: 'http://a.a' } }`
});

return loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
}).catch(err => {
expect(err.message).toMatch(/.*Unable to resolve project type.*/);
done();
});
});
});

describe("service name", () => {
it("lets config service name take precedence for client project", () => {});
it("lets name passed in take precedence over env var", () => {});
it("uses env var to determine service name when no other options", () => {});
it("lets config service name take precedence for client project", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { service: 'hello' } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js",
name: "not-it"
});

expect(config.client.service).toEqual("hello");
});

it("lets name passed in take precedence over env var", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js",
name: "hello"
});

expect(config.client.service).toEqual("hello");
});

it("uses env var to determine service name when no other options", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { } }`,
".env": `ENGINE_API_KEY=service:harambe:54378950jn`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.client.service).toEqual("harambe");
});
});

describe("default merging", () => {
it("merges service name and default config for client projects", () => {});
it("merges service name and default config for service projects", () => {});
it("merges engine config with projects", () => {});
it("merges defaults in at the end", () => {});
it("merges service name and default config for client projects", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { service: 'hello' } }`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.rawConfig.client.includes).toEqual(
DefaultClientConfig.includes
);
});

it("merges service name and default config for service projects", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { service: { name: 'wow' } }`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.rawConfig.service.includes).toEqual(
DefaultServiceConfig.includes
);
});

it("merges engine config defaults", async () => {
writeFilesToDir(dir, {
"my.config.js": `module.exports = { client: { service: 'wow' } }`
});

const config = await loadConfig({
configPath: dirPath,
configFileName: "my.config.js"
});

expect(config.rawConfig.engine.endpoint).toEqual(
DefaultEngineConfig.endpoint
);
});
});
});
11 changes: 8 additions & 3 deletions packages/apollo-language-server/src/config/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cosmiconfig from "cosmiconfig";
import { LoaderEntry } from "cosmiconfig";
import TypeScriptLoader from "@endemolshinegroup/cosmiconfig-typescript-loader";
import { resolve } from "path";
import { readFileSync, existsSync } from "fs";
import { readFileSync, existsSync, lstatSync } from "fs";
import { merge, get } from "lodash/fp";
import {
ApolloConfig,
Expand Down Expand Up @@ -116,7 +116,7 @@ export async function loadConfig({
? resolve(configPath, ".env")
: resolve(process.cwd(), ".env");

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

// if there wasn't a config loaded from a file, build one.
// if there was a service name found in the env, merge it with the new/existing config object.
if (!loadedConfig || serviceName) {
// if the config loaded doesn't have a client/service key, add one based on projectType
if (
!loadedConfig ||
serviceName ||
!(loadedConfig.config.client || loadedConfig.config.service)
) {
loadedConfig = {
filepath: configPath || process.cwd(),
config: {
Expand Down

0 comments on commit 877e953

Please sign in to comment.