mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 18:20:11 +01:00
refactor: split into packages and add tests
This commit is contained in:
parent
fd58344ca7
commit
11117e0c0e
28 changed files with 2736 additions and 636 deletions
112
internal/errors/errors_test.go
Normal file
112
internal/errors/errors_test.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNixBuildError(t *testing.T) {
|
||||
underlyingErr := errors.New("exec: \"nix\": executable file not found in $PATH")
|
||||
err := &NixBuildError{
|
||||
Derivation: "test.drv",
|
||||
Stderr: "some stderr output",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
|
||||
expectedMsg := "nix build for test.drv failed: exec: \"nix\": executable file not found in $PATH (stderr: some stderr output)"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNixNoOutputPathError(t *testing.T) {
|
||||
err := &NixNoOutputPathError{
|
||||
Derivation: "empty.drv",
|
||||
Stderr: "build successful, but no paths",
|
||||
}
|
||||
expectedMsg := "nix build for empty.drv produced no output path (stderr: build successful, but no paths)"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileReadError(t *testing.T) {
|
||||
underlyingErr := os.ErrPermission
|
||||
err := &FileReadError{
|
||||
Path: "/tmp/file.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to read file /tmp/file.json: permission denied"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONUnmarshalError(t *testing.T) {
|
||||
underlyingErr := errors.New("unexpected end of JSON input")
|
||||
err := &JSONUnmarshalError{
|
||||
Source: "/tmp/data.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to unmarshal JSON from /tmp/data.json: unexpected end of JSON input"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExecutionError(t *testing.T) {
|
||||
underlyingErr := errors.New("command timed out")
|
||||
err := &ScriptExecutionError{
|
||||
Path: "/tmp/script.sh",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "script /tmp/script.sh execution failed: command timed out"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotCreateError(t *testing.T) {
|
||||
underlyingErr := os.ErrExist
|
||||
err := &SnapshotCreateError{
|
||||
FilePath: "/snapshots/test.snap.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to create/update snapshot /snapshots/test.snap.json: file already exists"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotLoadError(t *testing.T) {
|
||||
underlyingErr := &JSONUnmarshalError{Source: "test.snap.json", Err: fmt.Errorf("bad json")}
|
||||
err := &SnapshotLoadError{
|
||||
FilePath: "/snapshots/test.snap.json",
|
||||
Err: underlyingErr,
|
||||
}
|
||||
expectedMsg := "failed to load/parse snapshot /snapshots/test.snap.json: failed to unmarshal JSON from test.snap.json: bad json"
|
||||
if err.Error() != expectedMsg {
|
||||
t.Errorf("Error() got %q, want %q", err.Error(), expectedMsg)
|
||||
}
|
||||
if !errors.Is(err, underlyingErr) {
|
||||
t.Errorf("Unwrap() failed, underlying error not found")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue