Skip to content

Commit

Permalink
Merge pull request #233 from fromanirh/modernize-errors
Browse files Browse the repository at this point in the history
janitorial: modernize error detection
  • Loading branch information
jaypipes committed Mar 9, 2021
2 parents b593e32 + 0f86788 commit 1a1bc99
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/gpu/gpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package gpu_test

import (
"errors"
"os"
"testing"

Expand All @@ -17,7 +18,7 @@ func TestGPU(t *testing.T) {
if _, ok := os.LookupEnv("GHW_TESTING_SKIP_GPU"); ok {
t.Skip("Skipping GPU tests.")
}
if _, err := os.Stat("/sys/class/drm"); os.IsNotExist(err) {
if _, err := os.Stat("/sys/class/drm"); errors.Is(err, os.ErrNotExist) {
t.Skip("Skipping GPU tests. The environment has no /sys/class/drm directory.")
}
info, err := gpu.New()
Expand Down
3 changes: 2 additions & 1 deletion pkg/memory/memory_cache_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package memory

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -62,7 +63,7 @@ func CachesForNode(ctx *context.Context, nodeID int) ([]*Cache, error) {
// directories contains information about the size of that level of
// cache and the processors mapped to it.
cachePath := filepath.Join(cpuPath, "cache")
if _, err = os.Stat(cachePath); os.IsNotExist(err) {
if _, err = os.Stat(cachePath); errors.Is(err, os.ErrNotExist) {
continue
}
cacheDirFiles, err := ioutil.ReadDir(cachePath)
Expand Down
3 changes: 2 additions & 1 deletion pkg/snapshot/clonetree_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package snapshot

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -120,7 +121,7 @@ func createBlockDeviceDir(buildDeviceDir string, srcDeviceDir string) error {
// and whether the device is read-only
buf, err := ioutil.ReadFile(fp)
if err != nil {
if os.IsPermission(err) {
if errors.Is(err, os.ErrPermission) {
// example: /sys/devices/virtual/block/zram0/compact is 0400
trace("permission denied reading %q - skipped\n", fp)
continue
Expand Down
3 changes: 2 additions & 1 deletion pkg/snapshot/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package snapshot
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand All @@ -35,7 +36,7 @@ func OpenDestination(snapshotName string) (*os.File, error) {
var f *os.File
var err error

if _, err = os.Stat(snapshotName); os.IsNotExist(err) {
if _, err = os.Stat(snapshotName); errors.Is(err, os.ErrNotExist) {
if f, err = os.Create(snapshotName); err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/snapshot/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package snapshot_test

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -33,7 +34,7 @@ func TestUnpack(t *testing.T) {
t.Fatalf("Expected nil err, but got %v", err)
}

if _, err := os.Stat(root); !os.IsNotExist(err) {
if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Expected %q to be gone, but still exists", root)
}
}
Expand All @@ -60,7 +61,7 @@ func TestUnpackInto(t *testing.T) {
t.Fatalf("Expected nil err, but got %v", err)
}

if _, err := os.Stat(testRoot); !os.IsNotExist(err) {
if _, err := os.Stat(testRoot); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Expected %q to be gone, but still exists", testRoot)
}
}
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestUnpackIntoPresrving(t *testing.T) {
t.Fatalf("Expected nil err, but got %v", err)
}

if _, err := os.Stat(testRoot); !os.IsNotExist(err) {
if _, err := os.Stat(testRoot); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Expected %q to be gone, but still exists", testRoot)
}
}
Expand Down

0 comments on commit 1a1bc99

Please sign in to comment.