Skip to content

Commit

Permalink
feat: Changing default to not use systemvars.
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
mrsteele committed Aug 8, 2016
1 parent 96d7536 commit 0c01c85
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 8 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
"version": "0.0.0-semantically-released",
"main": "index.js",
"scripts": {
"precommit": "npm run standard && npm run build && npm run cover",
"precommit": "npm run standard && npm run cover",
"commit": "git-cz",
"standard": "standard",
"predoc": "rimraf docs",
"doc": "jsdoc -c doc.json",
"cover": "nyc npm t && npm run coverage-report",
"coverage-check": "nyc check-coverage --branches 100 --statements 100 --functions 100 --lines 100",
"coverage-report": "nyc report --reporter=lcov",
"test": "_mocha --compilers js:babel-register",
"test": "npm run build && _mocha --compilers js:babel-register",
"prebuild": "rimraf dist",
"build": "babel --copy-files --out-dir dist src",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"travis": "npm run precommit",
"travis2": "npm run precommit && npm run coveralls && npm run coverage-check",
"travis": "npm run precommit && npm run coveralls",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
Expand Down Expand Up @@ -89,7 +87,8 @@
},
"nyc": {
"include": [
"src/**/*.js"
"src/**/*.js",
"dist/**/*.js"
]
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Dotenv {
path: './.env',
safe: false,
sample: './.env.example',
systemvars: true
systemvars: false
}, options)

let vars = (options.systemvars) ? Object.assign({}, process.env) : {}
const blueprint = (options.safe) ? this.loadFile(options.sample) : this.loadFile(options.path)
const env = this.loadFile(options.path)
const blueprint = (options.safe) ? this.loadFile(options.sample) : env

Object.keys(blueprint).map(key => {
const value = env[key] || env[key]
Expand Down
Empty file added test/envs/.empty
Empty file.
Empty file added test/envs/.empty.example
Empty file.
1 change: 1 addition & 0 deletions test/envs/.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST=testing
1 change: 1 addition & 0 deletions test/envs/.simple.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST=should exist
67 changes: 67 additions & 0 deletions test/main.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,78 @@
/* global describe, it */

// Tests suite
import path from 'path'
import chai from 'chai'
chai.should()

// The stars of the show
import Src from '../src'
import Dist from '../dist'
const envEmpty = path.resolve(__dirname, './envs/.empty')
const envEmptyExample = path.resolve(__dirname, './envs/.empty.example')
const envSimple = path.resolve(__dirname, './envs/.simple')
const envSimpleExample = path.resolve(__dirname, './envs/.simple.example')

const envEmptyJson = {}
const envSimpleJson = {TEST: 'testing'}

function runTests (Obj, name) {
function envTest (config) {
return JSON.parse(new Obj(config).definitions['process.env'])
}

describe(name, () => {
describe('Defaults', () => {
it('Should be a function.', () => {
Obj.should.be.a.function
})

// @todo - This one isn't a great test, but it wasn't really working for me.
it('Should return a instance of DefinePlugin.', () => {
envTest().should.be.an('object')
})

it('Should be an empty object when no environment variables exist in .env file.', () => {
envTest().should.deep.equal(envEmptyJson)
})
})

describe('Simple configuration', () => {
it('Should load enviornment variables when they exist in the .env file.', () => {
envTest({path: envSimple}).should.deep.equal(envSimpleJson)
})

it('Should allow system env variables', () => {
const test = envTest({path: envSimple, systemvars: true})
const key = Object.keys(envSimpleJson)[0]
const value = envSimpleJson[key]
test[key].should.equal(value)
Object.keys(test).length.should.be.above(Object.keys(envSimpleJson).length)
})
})

describe('Safe configuration', () => {
it('Should load successfully if variables defined', () => {
envTest({path: envEmpty, safe: true, sample: envEmptyExample}).should.deep.equal(envEmptyJson)
envTest({path: envSimple, safe: true, sample: envSimpleExample}).should.deep.equal(envSimpleJson)
})

it('Should fail if env does not match sample.', () => {
function errorTest () {
envTest({path: envEmpty, safe: true, sample: envSimpleExample})
}

errorTest.should.throw('Missing environment variable')
})
})
})
}

describe('The tests', () => {
it('Should be able to run tests.', () => {
true.should.be.true
})

runTests(Src, 'Source')
runTests(Dist, 'Distribution')
})

0 comments on commit 0c01c85

Please sign in to comment.