Skip to content

Commit

Permalink
Merge pull request #21 from farisj/jf-close-files
Browse files Browse the repository at this point in the history
Close Files after Writing
  • Loading branch information
farisj committed Oct 22, 2017
2 parents 6aeae76 + 08432c2 commit 1716e57
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/dotenvious/env_appender.rb
Expand Up @@ -6,6 +6,7 @@ def initialize(filename)

def append(key)
env.write("#{key}=#{ENV_EXAMPLE[key]}\n")
env.close
end

private
Expand Down
3 changes: 2 additions & 1 deletion lib/dotenvious/value_replacer.rb
Expand Up @@ -11,6 +11,7 @@ def replace(key)
updated_env = base_env.dup
updated_env[line_number] = "#{key}=#{ENV_EXAMPLE[key]}"
env_writer.write(updated_env.join("\n") + "\n")
env_writer.close
end

private
Expand All @@ -22,7 +23,7 @@ def base_env
end

def env_writer
File.open(filename, 'w')
@env_writer ||= File.open(filename, 'w')
end
end
end
1 change: 1 addition & 0 deletions spec/dotenvious/env_appender_spec.rb
Expand Up @@ -9,6 +9,7 @@
it 'appends the value to the end of the .env file' do
env_double = double('File', write: nil)
expect(env_double).to receive(:write).with("test2=example2\n")
expect(env_double).to receive(:close)
expect(File).to receive(:open).
with('.big-ol-env', 'a+').once.
and_return(env_double)
Expand Down
9 changes: 8 additions & 1 deletion spec/dotenvious/prompter_spec.rb
Expand Up @@ -16,9 +16,13 @@

it 'appends the vars to .env' do
expect(STDIN).to receive(:gets).twice.and_return('y','n')
file_double = double
expect(file_double).to receive(:write).with("test2=\n")
expect(file_double).to receive(:close)

expect(File).to receive(:open).
with('.env', 'a+').once.
and_return(double('File', write: nil))
and_return(file_double)

described_class.new.run
end
Expand All @@ -28,5 +32,8 @@

described_class.new.run
end

xit 'given missing and different args, appends/rewrites correctly into env' do
end
end
end
1 change: 1 addition & 0 deletions spec/dotenvious/value_replacer_spec.rb
Expand Up @@ -14,6 +14,7 @@

env_double = double('File', write: nil)
expect(env_double).to receive(:write).with("test=1234\nfake=correct\n")
expect(env_double).to receive(:close)

expect(File).to receive(:open).
with('.big-ol-env', 'w').
Expand Down

0 comments on commit 1716e57

Please sign in to comment.