From a160e167e42d89ecfe1316659982022966ccc594 Mon Sep 17 00:00:00 2001 From: Matt Loring Date: Tue, 5 Jan 2016 21:36:54 -0800 Subject: [PATCH] Support relative config file paths from cwd Fixes #198 --- index.js | 3 +- test/fixtures/test-config.json | 8 +++++ test/standalone/test-config-json.js | 35 ++++++++++++++++++ test/standalone/test-config-priority.js | 6 ++-- test/standalone/test-config-relative-path.js | 37 ++++++++++++++++++++ 5 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/test-config.json create mode 100644 test/standalone/test-config-json.js create mode 100644 test/standalone/test-config-relative-path.js diff --git a/index.js b/index.js index 4586d02da..fa424109a 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ var SpanData = require('./lib/span-data.js'); var common = require('@google/cloud-diagnostics-common'); var semver = require('semver'); var constants = require('./lib/constants.js'); +var path = require('path'); /** * Phantom implementation of the trace agent. This allows API users to decouple @@ -53,7 +54,7 @@ var initConfig = function(projectConfig) { util._extend(config, require('./config.js')); util._extend(config, projectConfig); if (process.env.hasOwnProperty('GCLOUD_TRACE_CONFIG')) { - util._extend(config, require(process.env.GCLOUD_TRACE_CONFIG)); + util._extend(config, require(path.resolve(process.env.GCLOUD_TRACE_CONFIG))); } if (process.env.hasOwnProperty('GCLOUD_TRACE_LOGLEVEL')) { config.logLevel = process.env.GCLOUD_TRACE_LOGLEVEL; diff --git a/test/fixtures/test-config.json b/test/fixtures/test-config.json new file mode 100644 index 000000000..e53126994 --- /dev/null +++ b/test/fixtures/test-config.json @@ -0,0 +1,8 @@ +{ + "logLevel": 4, + + "stackTraceLimit": 1, + + "flushDelaySeconds": 31 +} + diff --git a/test/standalone/test-config-json.js b/test/standalone/test-config-json.js new file mode 100644 index 000000000..ae951339a --- /dev/null +++ b/test/standalone/test-config-json.js @@ -0,0 +1,35 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * 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. + */ +'use strict'; + +var path = require('path'); +var assert = require('assert'); + +// Default configuration: +// { logLevel: 1, stackTraceLimit: 0, flushDelaySeconds: 30, samplingRate: 10 }; + +// Fixtures configuration: +// { logLevel: 4, stackTraceLimit: 1 }; +process.env.GCLOUD_TRACE_CONFIG = path.join('test', 'fixtures', 'test-config.json'); + +var agent = require('../..').start(); + +describe('json config', function() { + it('should load trace config from json file', function() { + var config = agent.private_().config_; + assert.equal(config.logLevel, 4); + }); +}); diff --git a/test/standalone/test-config-priority.js b/test/standalone/test-config-priority.js index fbad64dd4..d4ee9023f 100644 --- a/test/standalone/test-config-priority.js +++ b/test/standalone/test-config-priority.js @@ -22,11 +22,11 @@ var assert = require('assert'); // { logLevel: 1, stackTraceLimit: 0, flushDelaySeconds: 30, samplingRate: 10 }; // Fixtures configuration: -// { logLevel: 2, stackTraceLimit: 1 }; +// { logLevel: 4, stackTraceLimit: 1 }; process.env.GCLOUD_TRACE_CONFIG = path.join(__dirname, '..', 'fixtures', 'test-config.js'); -process.env.GCLOUD_TRACE_LOGLEVEL = 4; +process.env.GCLOUD_TRACE_LOGLEVEL = 2; var agent = require('../..').start({logLevel: 3, stackTraceLimit: 2, flushDelaySeconds: 31}); @@ -34,7 +34,7 @@ var agent = require('../..').start({logLevel: 3, stackTraceLimit: 2, describe('should respect config load order', function() { it('should order Default -> start -> env config -> env specific', function() { var config = agent.private_().config_; - assert.equal(config.logLevel, 4); + assert.equal(config.logLevel, 2); assert.equal(config.stackTraceLimit, 1); assert.equal(config.flushDelaySeconds, 31); assert.equal(config.samplingRate, 10); diff --git a/test/standalone/test-config-relative-path.js b/test/standalone/test-config-relative-path.js new file mode 100644 index 000000000..15fe62c6a --- /dev/null +++ b/test/standalone/test-config-relative-path.js @@ -0,0 +1,37 @@ +/** + * Copyright 2015 Google Inc. All Rights Reserved. + * + * 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. + */ +'use strict'; + +var path = require('path'); +var assert = require('assert'); + +// Default configuration: +// { logLevel: 1, stackTraceLimit: 0, flushDelaySeconds: 30, samplingRate: 10 }; + +// Fixtures configuration: +// { logLevel: 4, stackTraceLimit: 1 }; +process.env.GCLOUD_TRACE_CONFIG = path.join('fixtures', 'test-config.js'); + +process.chdir('test'); + +var agent = require('../..').start(); + +describe('relative config', function() { + it('should load trace config from relative path', function() { + var config = agent.private_().config_; + assert.equal(config.logLevel, 4); + }); +});