Skip to content

Commit

Permalink
Add proper .sops extension for file without extension (#51)
Browse files Browse the repository at this point in the history
* Add proper .sops extension for file without extension

* add more no-extension test cases

Co-authored-by: Justin Hart <[email protected]>
  • Loading branch information
randrusiak and onyxraven authored Sep 7, 2022
1 parent acfaa89 commit 5a95df7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ func NormalizeToSopsFile(fn string) string {
}

ext := path.Ext(fn)
fn = strings.Replace(fn, ext, sopsCryptedFileSegment+ext, 1)

if len(ext) > 0 {
fn = strings.Replace(fn, ext, sopsCryptedFileSegment+ext, 1)
} else {
fn = fn + sopsCryptedFileSegment
}

return fn
}
Expand Down
36 changes: 36 additions & 0 deletions fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestNormalizeToPlaintextFile(t *testing.T) {
{name: "Without the segment", args: args{fn: "filename.yaml"}, want: "filename.yaml"},
{name: "With the segment twice", args: args{fn: "filename.sops.something.sops.yaml"}, want: "filename.sops.something.yaml"},
{name: "With the segment last", args: args{fn: "something.bin.sops"}, want: "something.bin"},
{name: "With no extension", args: args{fn: "something.sops"}, want: "something"},
}

for _, tt := range tests {
Expand All @@ -47,6 +48,7 @@ func TestNormalizeToSopsFile(t *testing.T) {
{name: "With the segment twice", args: args{fn: "filename.sops.something.sops.yaml"}, want: "filename.sops.something.sops.yaml"},
{name: "With the segment in the wrong place", args: args{fn: "filename.sops.something.yaml"}, want: "filename.sops.something.sops.yaml"},
{name: "Ends with segment", args: args{fn: "filename.yaml.sops"}, want: "filename.yaml.sops"},
{name: "Without the extension", args: args{fn: "filename_no_ext"}, want: "filename_no_ext.sops"},
}

for _, tt := range tests {
Expand Down Expand Up @@ -101,6 +103,22 @@ func TestListIndexOf(t *testing.T) {
},
want: 0,
},
{
name: "no ext",
args: args{
files: []string{"filename.ext", "filename2.ext", "fnhere"},
fn: "fnhere",
},
want: 2,
},
{
name: "no ext normalized",
args: args{
files: []string{"filename.ext", "filename2.ext", "fnhere"},
fn: "fnhere.sops",
},
want: 2,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -150,6 +168,24 @@ func TestSomeOrAllFiles(t *testing.T) {
want: []string{"filename.ext"},
wantErr: false,
},
{
name: "one file no ext",
args: args{
args: []string{"filename"},
encFiles: []string{"filename", "filename2.ext", "filename3.ext"},
},
want: []string{"filename"},
wantErr: false,
},
{
name: "one file normalized no ext",
args: args{
args: []string{"filename.sops"},
encFiles: []string{"filename", "filename2.ext", "filename3.ext"},
},
want: []string{"filename"},
wantErr: false,
},
{
name: "two files",
args: args{
Expand Down

0 comments on commit 5a95df7

Please sign in to comment.