Skip to content

Commit

Permalink
Pass opts by reference
Browse files Browse the repository at this point in the history
The environment variable change was being lost due to passing by value.
Add a test to catch this as well.
  • Loading branch information
kruton authored and orgrim committed Sep 23, 2023
1 parent b8767d5 commit d29bd1c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func run() (retVal error) {
// the command line
opts := mergeCliAndConfigOptions(cliOpts, cliOptions, cliOptList)

err = ensureCipherParamsPresent(opts)
err = ensureCipherParamsPresent(&opts)
if err != nil {
return fmt.Errorf("required cipher parameters not present: %w", err)
}
Expand Down Expand Up @@ -696,7 +696,7 @@ func dumper(id int, jobs <-chan *dump, results chan<- *dump, fc chan<- sumFileJo
}
}

func ensureCipherParamsPresent(opts options) error {
func ensureCipherParamsPresent(opts *options) error {
// Nothing needs to be done if we are not encrypting or decrypting
if !opts.Encrypt && !opts.Decrypt {
return nil
Expand Down
50 changes: 50 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,53 @@ func TestExecPath(t *testing.T) {
})
}
}

func TestEnsureCipherParamsPresent_NoEncryptNoDecrypt_NoParams_ReturnsNil(t *testing.T) {
opts := options{}

err := ensureCipherParamsPresent(&opts)
if err != nil {
t.Errorf("should not return error")
}
}

func TestEnsureCipherParamsPresent_NoEncryptNoDecrypt_HasParams_ReturnsNil(t *testing.T) {
opts := options{
CipherPublicKey: "foo1",
CipherPrivateKey: "bar99",
CipherPassphrase: "secretwords",
}

err := ensureCipherParamsPresent(&opts)
if err != nil {
t.Errorf("should not return error")
}
}

func TestEnsureCipherParamsPresent_Encrypt_NoParams_Failure(t *testing.T) {
opts := options{
Encrypt: true,
CipherPrivateKey: "bar99",
}

err := ensureCipherParamsPresent(&opts)
if err == nil {
t.Errorf("should have error about not finding passphrase")
}
}

func TestEnsureCipherParamsPresent_Encrypt_NoParamsButEnv_Success(t *testing.T) {
opts := options{
Encrypt: true,
}
t.Setenv("PGBK_CIPHER_PASS", "works")

err := ensureCipherParamsPresent(&opts)
if err != nil {
t.Errorf("should have read environment variable")
}

if opts.CipherPassphrase != "works" {
t.Errorf("passphrase was not read correctly from environment")
}
}

0 comments on commit d29bd1c

Please sign in to comment.