Skip to content

Commit

Permalink
swapping positions and defaults for stub_command_with to make more se…
Browse files Browse the repository at this point in the history
…nse, could use bool instead, but having a symbol leaves stub open to other command formats in the future
  • Loading branch information
Kevin committed Jan 13, 2025
1 parent 56f1ec3 commit cf650fb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion test/secrets/aws_secrets_manager_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class AwsSecretsManagerAdapterTest < SecretAdapterTestCase
end

test "fetch without CLI installed" do
stub_command_with("aws --version", :system, false)
stub_command_with("aws --version", false)

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "SECRET1")))
Expand Down
2 changes: 1 addition & 1 deletion test/secrets/bitwarden_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class BitwardenAdapterTest < SecretAdapterTestCase
end

test "fetch without CLI installed" do
stub_command_with("bw --version", :system, false)
stub_command_with("bw --version", false, :system)

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "mynote")))
Expand Down
16 changes: 8 additions & 8 deletions test/secrets/doppler_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DopplerAdapterTest < SecretAdapterTestCase
end

test "fetch" do
stub_command_with("doppler --version", :system, true)
stub_command_with("doppler --version")
stub_command(:system).with("doppler me --json", err: File::NULL)

stub_command
Expand Down Expand Up @@ -47,7 +47,7 @@ class DopplerAdapterTest < SecretAdapterTestCase
test "fetch having DOPPLER_TOKEN" do
ENV["DOPPLER_TOKEN"] = "dp.st.xxxxxxxxxxxxxxxxxxxxxx"

stub_command_with("doppler --version", :system, true)
stub_command_with("doppler --version")
stub_command(:system).with("doppler me --json", err: File::NULL)

stub_command
Expand Down Expand Up @@ -88,7 +88,7 @@ class DopplerAdapterTest < SecretAdapterTestCase
end

test "fetch with folder in secret" do
stub_command_with("doppler --version", :system, true)
stub_command_with("doppler --version")
stub_command(:system).with("doppler me --json", err: File::NULL)

stub_command
Expand Down Expand Up @@ -127,7 +127,7 @@ class DopplerAdapterTest < SecretAdapterTestCase
end

test "fetch without --from" do
stub_command_with("doppler --version", :system, true)
stub_command_with("doppler --version")
stub_command(:system).with("doppler me --json", err: File::NULL)

error = assert_raises RuntimeError do
Expand All @@ -138,9 +138,9 @@ class DopplerAdapterTest < SecretAdapterTestCase
end

test "fetch with signin" do
stub_command_with("doppler --version", :system, true)
stub_command_with("doppler me --json", :system, false)
stub_command_with("doppler login -y", :`, true).returns("")
stub_command_with("doppler --version")
stub_command_with("doppler me --json", false)
stub_command_with("doppler login -y", true, :`).returns("")
stub_command.with("doppler secrets get SECRET1 --json -p my-project -c prd").returns(single_item_json)

json = JSON.parse(shellunescape(run_command("fetch", "--from", "my-project/prd", "SECRET1")))
Expand All @@ -153,7 +153,7 @@ class DopplerAdapterTest < SecretAdapterTestCase
end

test "fetch without CLI installed" do
stub_command_with("doppler --version", :system, false)
stub_command_with("doppler --version", false)

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "HOST", "PORT")))
Expand Down
6 changes: 3 additions & 3 deletions test/secrets/last_pass_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class LastPassAdapterTest < SecretAdapterTestCase
test "fetch with signin" do
stub_command(:system).with("lpass --version", err: File::NULL)

stub_command_with("lpass status --color never", :`, false).returns("Not logged in.")
stub_command_with("lpass login email@example.com", :`, true).returns("")
stub_command_with("lpass status --color never", false, :`).returns("Not logged in.")
stub_command_with("lpass login email@example.com", true, :`).returns("")
stub_command.with("lpass show SECRET1 --json").returns(single_item_json)

json = JSON.parse(shellunescape(run_command("fetch", "SECRET1")))
Expand All @@ -125,7 +125,7 @@ class LastPassAdapterTest < SecretAdapterTestCase
end

test "fetch without CLI installed" do
stub_command_with("lpass --version", :system, false)
stub_command_with("lpass --version", false)

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "SECRET1", "FOLDER1/FSECRET1", "FOLDER1/FSECRET2")))
Expand Down
10 changes: 5 additions & 5 deletions test/secrets/one_password_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ class SecretsOnePasswordAdapterTest < SecretAdapterTestCase
test "fetch with signin, no session" do
stub_command(:system).with("op --version", err: File::NULL)

stub_command_with("op account get --account myaccount", :system, false)
stub_command_with("op signin --account \"myaccount\" --force --raw", :`, true).returns("")
stub_command_with("op account get --account myaccount", false)
stub_command_with("op signin --account \"myaccount\" --force --raw", true, :`).returns("")

stub_command
.with("op item get myitem --vault \"myvault\" --fields \"label=section.SECRET1\" --format \"json\" --account \"myaccount\"")
Expand All @@ -138,8 +138,8 @@ class SecretsOnePasswordAdapterTest < SecretAdapterTestCase
test "fetch with signin and session" do
stub_command(:system).with("op --version", err: File::NULL)

stub_command_with("op account get --account myaccount", :system, false)
stub_command_with("op signin --account \"myaccount\" --force --raw", :`, true).returns("1234567890")
stub_command_with("op account get --account myaccount", false)
stub_command_with("op signin --account \"myaccount\" --force --raw", true, :`).returns("1234567890")

stub_command
.with("op item get myitem --vault \"myvault\" --fields \"label=section.SECRET1\" --format \"json\" --account \"myaccount\" --session \"1234567890\"")
Expand All @@ -155,7 +155,7 @@ class SecretsOnePasswordAdapterTest < SecretAdapterTestCase
end

test "fetch without CLI installed" do
stub_command_with("op --version", :system, false)
stub_command_with("op --version", false)

error = assert_raises RuntimeError do
JSON.parse(shellunescape(run_command("fetch", "--from", "op://myvault/myitem", "section/SECRET1", "section/SECRET2", "section2/SECRET3")))
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class SecretAdapterTestCase < ActiveSupport::TestCase

private
def stub_command(format = :`)
Kamal::Secrets::Adapters::Base.any_instance.stubs(format)
Kamal::Secrets::Adapters::Base.any_instance.stubs(:`)
end

def stub_command_with(command, format = :`, succeed = true)
def stub_command_with(command, succeed = true, format = :system)
# Sneakily run `exit 1`/`exit 0` after a match to set $? to 1/0
stub_command(format).with { |c| c == command && (succeed ? `exit 0` : `exit 1`) }
Kamal::Secrets::Adapters::Base.any_instance.stubs(format)
Expand Down

0 comments on commit cf650fb

Please sign in to comment.