Skip to content

Commit

Permalink
refactor: logx package and log validation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Jul 22, 2023
1 parent d8bd82a commit 139d244
Show file tree
Hide file tree
Showing 21 changed files with 291 additions and 112 deletions.
19 changes: 10 additions & 9 deletions cmd/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (

"sqlpkg.org/cli/assets"
"sqlpkg.org/cli/fileio"
"sqlpkg.org/cli/logx"
"sqlpkg.org/cli/spec"
)

// BuildAssetPath constructs an URL to download package asset.
func BuildAssetPath(pkg *spec.Package) (*spec.AssetPath, error) {
Debug("checking remote asset for platform %s-%s", runtime.GOOS, runtime.GOARCH)
Debug("asset base path = %s", pkg.Assets.Path)
logx.Debug("checking remote asset for platform %s-%s", runtime.GOOS, runtime.GOARCH)
logx.Debug("asset base path = %s", pkg.Assets.Path)

assetPath, err := pkg.AssetPath(runtime.GOOS, runtime.GOARCH)
if err != nil {
Expand All @@ -32,7 +33,7 @@ func BuildAssetPath(pkg *spec.Package) (*spec.AssetPath, error) {

// DownloadAsset downloads package asset.
func DownloadAsset(pkg *spec.Package, assetPath *spec.AssetPath) (*assets.Asset, error) {
Debug("downloading %s", assetPath)
logx.Debug("downloading %s", assetPath)
dir := spec.Dir(os.TempDir(), pkg.Owner, pkg.Name)
err := fileio.CreateDir(dir)
if err != nil {
Expand All @@ -50,15 +51,15 @@ func DownloadAsset(pkg *spec.Package, assetPath *spec.AssetPath) (*assets.Asset,
}

sizeKb := float64(asset.Size) / 1024
Debug("downloaded %s (%.2f Kb)", asset.Name, sizeKb)
logx.Debug("downloaded %s (%.2f Kb)", asset.Name, sizeKb)
return asset, nil
}

// ValidateAsset checks if the asset is valid.
func ValidateAsset(pkg *spec.Package, asset *assets.Asset) error {
checksumStr, ok := pkg.Assets.Checksums[asset.Name]
if !ok {
Debug("spec is missing asset checksum")
logx.Debug("spec is missing asset checksum")
return nil
}

Expand All @@ -71,7 +72,7 @@ func ValidateAsset(pkg *spec.Package, asset *assets.Asset) error {
return fmt.Errorf("asset checksum is invalid")
}

Debug("asset checksum is valid")
logx.Debug("asset checksum is valid")
return nil
}

Expand All @@ -82,14 +83,14 @@ func UnpackAsset(pkg *spec.Package, asset *assets.Asset) error {
return fmt.Errorf("failed to unpack asset: %w", err)
}
if nFiles == 0 {
Debug("not an archive, skipping unpack: %s", asset.Name)
logx.Debug("not an archive, skipping unpack: %s", asset.Name)
return nil
}
err = os.Remove(asset.Path)
if err != nil {
return fmt.Errorf("failed to delete asset after unpacking: %w", err)
}
Debug("unpacked %d files from %s", nFiles, asset.Name)
logx.Debug("unpacked %d files from %s", nFiles, asset.Name)
return nil
}

Expand Down Expand Up @@ -131,6 +132,6 @@ func DequarantineFiles(pkg *spec.Package) error {
return fmt.Errorf("failed to dequarantine files: %w", allErr)
}

Debug("removed %d files from quarantine", len(paths))
logx.Debug("removed %d files from quarantine", len(paths))
return nil
}
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"sqlpkg.org/cli/fileio"
"sqlpkg.org/cli/logx"
"sqlpkg.org/cli/spec"
)

Expand Down Expand Up @@ -55,6 +56,6 @@ func GetDirByFullName(fullName string) (string, error) {
// PrintLocalRepo prints information about the local sqlpkg repository.
func PrintLocalRepo() {
if WorkDir == "." {
Log("(local repository)")
logx.Log("(local repository)")
}
}
6 changes: 3 additions & 3 deletions cmd/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"text/tabwriter"

"sqlpkg.org/cli/cmd"
"sqlpkg.org/cli/logx"
)

const helpHelp = "usage: sqlpkg help"
Expand All @@ -32,8 +32,8 @@ func Help(args []string) error {
return errors.New(helpHelp)
}

cmd.Log("`sqlpkg` is an SQLite package manager. Use it to install or update SQLite extensions.\n")
cmd.Log("Commands:")
logx.Log("`sqlpkg` is an SQLite package manager. Use it to install or update SQLite extensions.\n")
logx.Log("Commands:")
w := tabwriter.NewWriter(os.Stdout, 0, 4, 0, ' ', 0)
for _, cmd := range commands {
fmt.Fprintln(w, cmd, "\t", commandsHelp[cmd])
Expand Down
7 changes: 4 additions & 3 deletions cmd/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"sqlpkg.org/cli/cmd"
"sqlpkg.org/cli/fileio"
"sqlpkg.org/cli/logx"
"sqlpkg.org/cli/spec"
)

Expand All @@ -20,13 +21,13 @@ func Info(args []string) error {
path := args[0]
pkg, err := cmd.FindSpec(path)
if err != nil {
cmd.Debug(err.Error())
cmd.Log("package not found")
logx.Debug(err.Error())
logx.Log("package not found")
return nil
}

lines := prepareInfo(pkg)
cmd.Log(strings.Join(lines, "\n"))
logx.Log(strings.Join(lines, "\n"))

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"os"

"sqlpkg.org/cli/cmd"
"sqlpkg.org/cli/fileio"
"sqlpkg.org/cli/logx"
"sqlpkg.org/cli/spec"
)

Expand All @@ -27,6 +27,6 @@ func Init(args []string) error {
return fmt.Errorf("failed to create a local repository: %w", err)
}

cmd.Log("✓ created a local repository")
logx.Log("✓ created a local repository")
return nil
}
25 changes: 13 additions & 12 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"sqlpkg.org/cli/cmd"
"sqlpkg.org/cli/logx"
"sqlpkg.org/cli/spec"
)

Expand All @@ -18,10 +19,10 @@ func InstallAll(args []string) error {
if err != nil {
return err
}
cmd.Debug("loaded the lockfile with %d packages", len(lck.Packages))
logx.Debug("loaded the lockfile with %d packages", len(lck.Packages))

if len(lck.Packages) == 0 {
cmd.Log("no packages found in the lockfile")
logx.Log("no packages found in the lockfile")
return nil
}

Expand All @@ -30,14 +31,14 @@ func InstallAll(args []string) error {
err = installLockedPackage(pkg)
if err != nil {
errCount += 1
cmd.Log("! %s", err)
logx.Log("! %s", err)
}
}

if errCount > 0 {
return fmt.Errorf("failed to install %d packages", errCount)
}
cmd.Log("installed %d packages", len(lck.Packages))
logx.Log("installed %d packages", len(lck.Packages))
return nil
}

Expand All @@ -56,7 +57,7 @@ func Install(args []string) error {

// installPackage installs a package using a specfile from a given path.
func installPackage(path string) error {
cmd.Log("> installing %s...", path)
logx.Log("> installing %s...", path)

pkg, err := cmd.ReadSpec(path)
if err != nil {
Expand All @@ -69,7 +70,7 @@ func installPackage(path string) error {
}

if !cmd.HasNewVersion(pkg) {
cmd.Log("✓ already at the latest version")
logx.Log("✓ already at the latest version")
return nil
}

Expand Down Expand Up @@ -119,32 +120,32 @@ func installPackage(path string) error {
}

dir := spec.Dir(cmd.WorkDir, pkg.Owner, pkg.Name)
cmd.Log("✓ installed package %s to %s", pkg.FullName(), dir)
logx.Log("✓ installed package %s to %s", pkg.FullName(), dir)
return nil
}

// installLockedPackage installs a specific version of a package from the lockfile.
func installLockedPackage(lckPkg *spec.Package) error {
path := lckPkg.Specfile
if path == "" {
cmd.Debug("missing specfile for %s, falling back to name/owner", lckPkg.FullName())
logx.Debug("missing specfile for %s, falling back to name/owner", lckPkg.FullName())
path = lckPkg.FullName()
}

cmd.Log("> installing %s...", path)
logx.Log("> installing %s...", path)

pkg, err := cmd.ReadSpec(path)
if err != nil {
return err
}

// lock the version
cmd.Debug("locked version = %s", lckPkg.Version)
logx.Debug("locked version = %s", lckPkg.Version)
pkg.Version = lckPkg.Version
pkg.Assets = lckPkg.Assets

if !cmd.HasNewVersion(pkg) {
cmd.Log("✓ already at the %s version", pkg.Version)
logx.Log("✓ already at the %s version", pkg.Version)
return nil
}

Expand Down Expand Up @@ -182,6 +183,6 @@ func installLockedPackage(lckPkg *spec.Package) error {
// it's already there

dir := spec.Dir(cmd.WorkDir, pkg.Owner, pkg.Name)
cmd.Log("✓ installed package %s to %s", pkg.FullName(), dir)
logx.Log("✓ installed package %s to %s", pkg.FullName(), dir)
return nil
}
29 changes: 24 additions & 5 deletions cmd/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,42 @@ import (
"sqlpkg.org/cli/cmd"
"sqlpkg.org/cli/fileio"
"sqlpkg.org/cli/lockfile"
"sqlpkg.org/cli/logx"
)

func TestInstall(t *testing.T) {
cmd.WorkDir = "."
repoDir, lockPath := cmd.SetupTestRepo(t)
memory := cmd.SetupTestLogger()

pkgDir := filepath.Join(repoDir, "nalgeon", "example")
args := []string{filepath.Join(cmd.WorkDir, "testdata", "sqlpkg.json")}
cmd.IsVerbose = true

err := Install(args)
if err != nil {
t.Fatalf("installation error: %v", err)
}

validateLog(t, memory)
validatePackage(t, repoDir, lockPath, "nalgeon", "example")

cmd.TeardownTestRepo(t, repoDir, lockPath)
}

func validateLog(t *testing.T, mem *logx.Memory) {
mem.Print()
mem.MustHave(t, "installing testdata/sqlpkg.json")
mem.MustHave(t, "read package nalgeon/example, version = 0.1.0")
mem.MustHave(t, "read 4 checksums")
mem.MustHave(t, "downloaded example-")
mem.MustHave(t, "asset checksum is valid")
mem.MustHave(t, "unpacked 1 files")
mem.MustHave(t, "created new lockfile")
mem.MustHave(t, "added package to the lockfile")
mem.MustHave(t, "installed package nalgeon/example")
}

func validatePackage(t *testing.T, repoDir, lockPath, owner, name string) {
pkgDir := filepath.Join(repoDir, owner, name)

if !fileio.Exists(pkgDir) {
t.Fatalf("package dir does not exist: %v", pkgDir)
}
Expand Down Expand Up @@ -53,6 +74,4 @@ func TestInstall(t *testing.T) {
if pkg.Assets.Files["linux-amd64"] != "example-linux-0.1.0-x86.zip" {
t.Fatalf("unexpected linux asset: %s", pkg.Assets.Files["linux-amd64"])
}

cmd.TeardownTestRepo(t, repoDir, lockPath)
}
8 changes: 4 additions & 4 deletions cmd/install/testdata/checksums.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
6bc24897dde2c7f00cf435055a6853358cb06fcb5a2a789877903ebec0b9298d example-linux-x86.zip
e3de533fdc23e0d953572c2b544ecc2951b890758af0a00b5a42695ae59ee7ac example-macos-arm64.zip
e3de533fdc23e0d953572c2b544ecc2951b890758af0a00b5a42695ae59ee7ac example-macos-x86.zip
f0d2d705bbe641bf2950a51253820e85de04373b7f428f109f69df1d85fa0654 example-win-x64.zip
6bc24897dde2c7f00cf435055a6853358cb06fcb5a2a789877903ebec0b9298d example-linux-0.1.0-x86.zip
e3de533fdc23e0d953572c2b544ecc2951b890758af0a00b5a42695ae59ee7ac example-macos-0.1.0-arm64.zip
e3de533fdc23e0d953572c2b544ecc2951b890758af0a00b5a42695ae59ee7ac example-macos-0.1.0-x86.zip
f0d2d705bbe641bf2950a51253820e85de04373b7f428f109f69df1d85fa0654 example-win-0.1.0-x64.zip
7 changes: 4 additions & 3 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"sqlpkg.org/cli/cmd"
"sqlpkg.org/cli/lockfile"
"sqlpkg.org/cli/logx"
"sqlpkg.org/cli/spec"
)

Expand Down Expand Up @@ -55,7 +56,7 @@ func gatherPackages() ([]*spec.Package, error) {
packages = append(packages, pkg)
}

cmd.Debug("gathered %d packages", len(packages))
logx.Debug("gathered %d packages", len(packages))
return packages, nil
}

Expand All @@ -79,7 +80,7 @@ func addMissingToLockfile(lck *lockfile.Lockfile, packages []*spec.Package) erro
return fmt.Errorf("failed to save lockfile: %w", err)
}

cmd.Debug("added %d packages to the lockfile", count)
logx.Debug("added %d packages to the lockfile", count)
return nil
}

Expand All @@ -94,7 +95,7 @@ func sortPackages(packages []*spec.Package) {
func printPackages(packages []*spec.Package) {
cmd.PrintLocalRepo()
if len(packages) == 0 {
cmd.Log("no packages installed")
logx.Log("no packages installed")
return
}

Expand Down
Loading

0 comments on commit 139d244

Please sign in to comment.