forked from sigstore/cosign
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix panic when os.Stat returns an error besides ErrNotExists (sigstor…
…e#2162) * fix panic when os.Stat returns an error besides ErrNotExists Signed-off-by: Samsondeen Dare <[email protected]> * boilerplate fix Signed-off-by: Samsondeen Dare <[email protected]> * move FileExists to internal package Signed-off-by: Samsondeen Dare <[email protected]> * remove archived pkg/errors Signed-off-by: Samsondeen Dare <[email protected]> * comments Signed-off-by: Samsondeen Dare <[email protected]> * comments Signed-off-by: Samsondeen Dare <[email protected]> Signed-off-by: Samsondeen Dare <[email protected]>
- Loading branch information
Showing
5 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2022 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cosign | ||
|
||
import "os" | ||
|
||
func FileExists(filename string) (bool, error) { | ||
info, err := os.Stat(filename) | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
return !info.IsDir(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright 2022 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cosign | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func Test_FileExists(t *testing.T) { | ||
tmpFile, err := os.CreateTemp(os.TempDir(), "cosign_test.txt") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
exists bool | ||
wantErr bool | ||
}{ | ||
{"file exists", tmpFile.Name(), true, false}, | ||
{"file does not exist", "testt.txt", false, false}, | ||
{"other error e.g cannot access file", "\000x", false, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := FileExists(tt.path) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("FileExists() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.exists { | ||
t.Errorf("FileExists() = %v, want %v", got, tt.exists) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters