-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add install method to plugin CLIManager (#364)
Signed-off-by: Patrick Zheng <[email protected]>
- Loading branch information
1 parent
966c6b7
commit 85a5bb9
Showing
14 changed files
with
1,135 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
// Copyright The Notary Project 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 file | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestCopyToDir(t *testing.T) { | ||
t.Run("copy file", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
data := []byte("data") | ||
filename := filepath.Join(tempDir, "a", "file.txt") | ||
if err := writeFile(filename, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
destDir := filepath.Join(tempDir, "b") | ||
if err := CopyToDir(filename, destDir); err != nil { | ||
t.Fatal(err) | ||
} | ||
}) | ||
|
||
t.Run("source directory permission error", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
|
||
tempDir := t.TempDir() | ||
destDir := t.TempDir() | ||
data := []byte("data") | ||
filename := filepath.Join(tempDir, "a", "file.txt") | ||
if err := writeFile(filename, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.Chmod(tempDir, 0000); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Chmod(tempDir, 0700) | ||
|
||
if err := CopyToDir(filename, destDir); err == nil { | ||
t.Fatal("should have error") | ||
} | ||
}) | ||
|
||
t.Run("not a regular file", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
destDir := t.TempDir() | ||
if err := CopyToDir(tempDir, destDir); err == nil { | ||
t.Fatal("should have error") | ||
} | ||
}) | ||
|
||
t.Run("source file permission error", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
|
||
tempDir := t.TempDir() | ||
destDir := t.TempDir() | ||
data := []byte("data") | ||
// prepare file | ||
filename := filepath.Join(tempDir, "a", "file.txt") | ||
if err := writeFile(filename, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
// forbid reading | ||
if err := os.Chmod(filename, 0000); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Chmod(filename, 0600) | ||
if err := CopyToDir(filename, destDir); err == nil { | ||
t.Fatal("should have error") | ||
} | ||
}) | ||
|
||
t.Run("dest directory permission error", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
|
||
tempDir := t.TempDir() | ||
destTempDir := t.TempDir() | ||
data := []byte("data") | ||
// prepare file | ||
filename := filepath.Join(tempDir, "a", "file.txt") | ||
if err := writeFile(filename, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
// forbid dest directory operation | ||
if err := os.Chmod(destTempDir, 0000); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Chmod(destTempDir, 0700) | ||
if err := CopyToDir(filename, filepath.Join(destTempDir, "a")); err == nil { | ||
t.Fatal("should have error") | ||
} | ||
}) | ||
|
||
t.Run("dest directory permission error 2", func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
|
||
tempDir := t.TempDir() | ||
destTempDir := t.TempDir() | ||
data := []byte("data") | ||
// prepare file | ||
filename := filepath.Join(tempDir, "a", "file.txt") | ||
if err := writeFile(filename, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
// forbid writing to destTempDir | ||
if err := os.Chmod(destTempDir, 0000); err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Chmod(destTempDir, 0700) | ||
if err := CopyToDir(filename, destTempDir); err == nil { | ||
t.Fatal("should have error") | ||
} | ||
}) | ||
|
||
t.Run("copy file and check content", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
data := []byte("data") | ||
filename := filepath.Join(tempDir, "a", "file.txt") | ||
if err := writeFile(filename, data); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
destDir := filepath.Join(tempDir, "b") | ||
if err := CopyToDir(filename, destDir); err != nil { | ||
t.Fatal(err) | ||
} | ||
validFileContent(t, filepath.Join(destDir, "file.txt"), data) | ||
}) | ||
} | ||
|
||
func TestFileNameWithoutExtension(t *testing.T) { | ||
input := "testfile.tar.gz" | ||
expectedOutput := "testfile.tar" | ||
actualOutput := TrimFileExtension(input) | ||
if actualOutput != expectedOutput { | ||
t.Errorf("expected '%s', but got '%s'", expectedOutput, actualOutput) | ||
} | ||
} | ||
|
||
func validFileContent(t *testing.T, filename string, content []byte) { | ||
b, err := os.ReadFile(filename) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(content, b) { | ||
t.Fatal("file content is not correct") | ||
} | ||
} | ||
|
||
func writeFile(path string, data []byte) error { | ||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { | ||
return err | ||
} | ||
return os.WriteFile(path, data, 0600) | ||
} |
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,49 @@ | ||
// Copyright The Notary Project 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 semver provides functions related to semanic version. | ||
// This package is based on "golang.org/x/mod/semver" | ||
package semver | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"golang.org/x/mod/semver" | ||
) | ||
|
||
// semVerRegEx is taken from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
var semVerRegEx = regexp.MustCompile(`^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`) | ||
|
||
// IsValid returns true if version is a valid semantic version | ||
func IsValid(version string) bool { | ||
return semVerRegEx.MatchString(version) | ||
} | ||
|
||
// ComparePluginVersion validates and compares two plugin semantic versions. | ||
// | ||
// The result will be 0 if v == w, -1 if v < w, or +1 if v > w. | ||
func ComparePluginVersion(v, w string) (int, error) { | ||
// sanity check | ||
if !IsValid(v) { | ||
return 0, fmt.Errorf("%s is not a valid semantic version", v) | ||
} | ||
if !IsValid(w) { | ||
return 0, fmt.Errorf("%s is not a valid semantic version", w) | ||
} | ||
|
||
// golang.org/x/mod/semver requires semantic version strings must begin | ||
// with a leading "v". Adding prefix "v" to the inputs. | ||
// Reference: https://pkg.go.dev/golang.org/x/mod/semver#pkg-overview | ||
return semver.Compare("v"+v, "v"+w), 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,40 @@ | ||
// Copyright The Notary Project 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 semver | ||
|
||
import "testing" | ||
|
||
func TestComparePluginVersion(t *testing.T) { | ||
t.Run("compare with lower version", func(t *testing.T) { | ||
comp, err := ComparePluginVersion("1.0.0", "1.0.1") | ||
if err != nil || comp >= 0 { | ||
t.Fatal("expected nil err and negative comp") | ||
} | ||
}) | ||
|
||
t.Run("compare with equal version", func(t *testing.T) { | ||
comp, err := ComparePluginVersion("1.0.1", "1.0.1") | ||
if err != nil || comp != 0 { | ||
t.Fatal("expected nil err and comp equal to 0") | ||
} | ||
}) | ||
|
||
t.Run("failed due to invalid semantic version", func(t *testing.T) { | ||
expectedErrMsg := "v1.0.0 is not a valid semantic version" | ||
_, err := ComparePluginVersion("v1.0.0", "1.0.1") | ||
if err == nil || err.Error() != expectedErrMsg { | ||
t.Fatalf("expected err %s, but got %s", expectedErrMsg, err) | ||
} | ||
}) | ||
} |
Oops, something went wrong.