diff --git a/lib/dotenvious/env_appender.rb b/lib/dotenvious/env_appender.rb index 5e9554c..fffc147 100644 --- a/lib/dotenvious/env_appender.rb +++ b/lib/dotenvious/env_appender.rb @@ -6,6 +6,7 @@ def initialize(filename) def append(key) env.write("#{key}=#{ENV_EXAMPLE[key]}\n") + env.close end private diff --git a/lib/dotenvious/value_replacer.rb b/lib/dotenvious/value_replacer.rb index 95022ce..699bb56 100644 --- a/lib/dotenvious/value_replacer.rb +++ b/lib/dotenvious/value_replacer.rb @@ -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 @@ -22,7 +23,7 @@ def base_env end def env_writer - File.open(filename, 'w') + @env_writer ||= File.open(filename, 'w') end end end diff --git a/spec/dotenvious/env_appender_spec.rb b/spec/dotenvious/env_appender_spec.rb index 040f3ea..113fe56 100644 --- a/spec/dotenvious/env_appender_spec.rb +++ b/spec/dotenvious/env_appender_spec.rb @@ -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) diff --git a/spec/dotenvious/prompter_spec.rb b/spec/dotenvious/prompter_spec.rb index 3028b37..bff90ba 100644 --- a/spec/dotenvious/prompter_spec.rb +++ b/spec/dotenvious/prompter_spec.rb @@ -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 @@ -28,5 +32,8 @@ described_class.new.run end + + xit 'given missing and different args, appends/rewrites correctly into env' do + end end end diff --git a/spec/dotenvious/value_replacer_spec.rb b/spec/dotenvious/value_replacer_spec.rb index 8b47f96..8f31cce 100644 --- a/spec/dotenvious/value_replacer_spec.rb +++ b/spec/dotenvious/value_replacer_spec.rb @@ -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').