From 1db33147c668f278117e275bae6209d7865c73d7 Mon Sep 17 00:00:00 2001 From: Mars Hall Date: Mon, 24 Oct 2016 13:18:08 -0700 Subject: [PATCH] Preserve bundle-size; backward-compatible with unpadded placeholder. (#4) * Preserve bundle-size; backward-compatible with unpadded placeholder. * Preserve size of bundle in characters instead of bytes. --- lib/injectable_env.rb | 9 ++++-- spec/injectable_env_spec.rb | 58 ++++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/lib/injectable_env.rb b/lib/injectable_env.rb index 17c544a..d1dba16 100644 --- a/lib/injectable_env.rb +++ b/lib/injectable_env.rb @@ -3,7 +3,7 @@ class InjectableEnv DefaultVarMatcher = /^REACT_APP_/ - Placeholder='{{REACT_APP_VARS_AS_JSON}}' + Placeholder = /\{\{REACT_APP_VARS_AS_JSON_*?\}\}/ def self.create(var_matcher=DefaultVarMatcher) vars = ENV.find_all {|name,value| var_matcher===name } @@ -25,10 +25,15 @@ def self.render(*args) def self.replace(file, *args) injectee = IO.read(file) - return unless injectee.index(Placeholder) + return unless placeholder = injectee.match(Placeholder) + placeholder_size = placeholder.to_s.size env = create(*args) + env_size = env.size + new_padding = placeholder_size - env_size + env = env + (' ' * [new_padding, 0].max) head,_,tail = injectee.partition(Placeholder) + injected = head + env + tail File.open(file, 'w') do |f| f.write(injected) diff --git a/spec/injectable_env_spec.rb b/spec/injectable_env_spec.rb index d049f21..129f9f2 100644 --- a/spec/injectable_env_spec.rb +++ b/spec/injectable_env_spec.rb @@ -5,6 +5,9 @@ RSpec.describe InjectableEnv do + Placeholder = '{{REACT_APP_VARS_AS_JSON______________________________________________________________________________________________________}}' + UnpaddedPlaceholder = '{{REACT_APP_VARS_AS_JSON}}' + describe '.create' do it "returns empty object" do expect(InjectableEnv.create).to eq('{}') @@ -63,7 +66,7 @@ describe '.replace' do before do - ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today" + ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today 🌞" end after do ENV.delete 'REACT_APP_HELLO' @@ -72,14 +75,61 @@ it "writes into file" do begin file = Tempfile.new('injectable_env_test') - file.write('var injected="{{REACT_APP_VARS_AS_JSON}}"') + file.write(%{var injected="#{Placeholder}"}) file.rewind InjectableEnv.replace(file.path) - expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today\\"}"' + expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}' actual_value=file.read - expect(actual_value).to eq(expected_value) + expect(actual_value.index(expected_value)).to eq(0) + # Closing double-quote is padded out but still last char. + actual_size = actual_value.size + expect(actual_value.index(/\"\Z/)).to eq(actual_size-1) + ensure + if file + file.close + file.unlink + end + end + end + + it "matches unpadded placeholder" do + begin + file = Tempfile.new('injectable_env_test') + file.write(%{var injected="#{UnpaddedPlaceholder}"}) + file.rewind + + InjectableEnv.replace(file.path) + + expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}' + actual_value=file.read + expect(actual_value.index(expected_value)).to eq(0) + # Closing double-quote is padded out but still last char. + actual_size = actual_value.size + expect(actual_value.index(/\"\Z/)).to eq(actual_size-1) + ensure + if file + file.close + file.unlink + end + end + end + + it "preserves character length of bundle" do + begin + placeholder_size = Placeholder.size + file = Tempfile.new('injectable_env_test') + file.write(Placeholder) + file.rewind + + InjectableEnv.replace(file.path) + + expected_value = '{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}' + actual_value = file.read + replaced_size = actual_value.size + expect(replaced_size).to eq(placeholder_size) + expect(actual_value.index(expected_value)).to eq(0) ensure if file file.close