|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRenderERB(t *testing.T) { |
| 10 | + t.Setenv("PGDATABASE", "noticehub-app-dev") |
| 11 | + t.Setenv("PGHOST", "db.internal") |
| 12 | + // PGUSER and PGPASSWORD are intentionally left unset to exercise defaults. |
| 13 | + |
| 14 | + cases := []struct { |
| 15 | + name string |
| 16 | + in string |
| 17 | + out string |
| 18 | + }{ |
| 19 | + {"fetch with block default, env set", `<%= ENV.fetch("PGDATABASE") { "fallback" } %>`, "noticehub-app-dev"}, |
| 20 | + {"fetch with block default, env unset", `<%= ENV.fetch("PGUSER") { "localhost" } %>`, "localhost"}, |
| 21 | + {"fetch with empty block default", `<%= ENV.fetch("PGPASSWORD") { "" } %>`, ""}, |
| 22 | + {"fetch with bare integer default", `<%= ENV.fetch("RAILS_MAX_THREADS") { 10 } %>`, "10"}, |
| 23 | + {"fetch with argument default", `<%= ENV.fetch("PGUSER", "postgres") %>`, "postgres"}, |
| 24 | + {"fetch without default, env unset", `<%= ENV.fetch("PGUSER") %>`, ""}, |
| 25 | + {"index, env set", `<%= ENV["PGHOST"] %>`, "db.internal"}, |
| 26 | + {"index, env unset", `<%= ENV["MISSING"] %>`, ""}, |
| 27 | + {"index with || fallback, env set", `<%= ENV["PGHOST"] || "localhost" %>`, "db.internal"}, |
| 28 | + {"index with || fallback, env unset", `<%= ENV["MISSING"] || "localhost" %>`, "localhost"}, |
| 29 | + {"index with || fallback, single quotes", `<%= ENV['MISSING'] || 'fallback' %>`, "fallback"}, |
| 30 | + {"single quotes", `<%= ENV.fetch('PGHOST') { 'x' } %>`, "db.internal"}, |
| 31 | + {"trim markers", `<%=- ENV.fetch("PGHOST") { "x" } -%>`, "db.internal"}, |
| 32 | + {"comment tag stripped", `before<%# secret %>after`, "beforeafter"}, |
| 33 | + {"no erb passes through", "database: plain-name", "database: plain-name"}, |
| 34 | + {"unrecognised expr left verbatim", `<%= Rails.env %>`, `<%= Rails.env %>`}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tc := range cases { |
| 38 | + t.Run(tc.name, func(t *testing.T) { |
| 39 | + out, _ := renderERB(tc.in) |
| 40 | + assert.Equal(t, tc.out, out) |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestRenderERBReportsUnresolved(t *testing.T) { |
| 46 | + t.Setenv("PGHOST", "db.internal") |
| 47 | + |
| 48 | + out, unresolved := renderERB(`a: <%= ENV["PGHOST"] %> |
| 49 | +b: <%= Rails.application.credentials.dig(:db) %> |
| 50 | +c: <%= ENV["MISSING"] || "ok" %>`) |
| 51 | + |
| 52 | + assert.Equal(t, "a: db.internal\nb: <%= Rails.application.credentials.dig(:db) %>\nc: ok", out) |
| 53 | + assert.Equal(t, []string{`<%= Rails.application.credentials.dig(:db) %>`}, unresolved) |
| 54 | +} |
| 55 | + |
| 56 | +func TestRenderERBFullConfig(t *testing.T) { |
| 57 | + t.Setenv("PGDATABASE", "noticehub-app-dev") |
| 58 | + |
| 59 | + in := `development: |
| 60 | + host: "localhost" |
| 61 | + database: <%= ENV.fetch("PGDATABASE") { "noticehub-app-dev" } %> |
| 62 | + username: <%= ENV.fetch("PGUSER") { "" } %> |
| 63 | + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 10 } %> |
| 64 | +` |
| 65 | + out := "development:\n" + |
| 66 | + " host: \"localhost\"\n" + |
| 67 | + " database: noticehub-app-dev\n" + |
| 68 | + " username: \n" + // empty value leaves the trailing space; YAML reads it as nil |
| 69 | + " pool: 10\n" |
| 70 | + rendered, _ := renderERB(in) |
| 71 | + assert.Equal(t, out, rendered) |
| 72 | +} |
0 commit comments