Files
ThothII/tools/tht/internal/lifecycle/lock_test.go
T

51 lines
1.4 KiB
Go

package lifecycle
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/aritmolab/thothii/tools/tht/internal/config"
)
func TestLifecycleLockIsExclusivePerInstallationAndReusableAfterRelease(t *testing.T) {
installation := config.Installation{ProjectDirectory: t.TempDir(), Path: filepath.Join(t.TempDir(), "thothii-installation.yaml")}
first, err := Acquire(installation)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = first.Release() })
if _, err := Acquire(installation); !errors.Is(err, ErrLocked) {
t.Fatalf("second Acquire() error = %v, want ErrLocked", err)
}
if err := first.Release(); err != nil {
t.Fatal(err)
}
second, err := Acquire(installation)
if err != nil {
t.Fatalf("Acquire() after release: %v", err)
}
if err := second.Release(); err != nil {
t.Fatal(err)
}
}
func TestLifecycleLockReleaseDoesNotRemoveAnotherOwnersFile(t *testing.T) {
installation := config.Installation{ProjectDirectory: t.TempDir(), Path: filepath.Join(t.TempDir(), "thothii-installation.yaml")}
lock, err := Acquire(installation)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(lock.Path(), []byte(`{"token":"different-owner"}`), 0o600); err != nil {
t.Fatal(err)
}
if err := lock.Release(); !errors.Is(err, ErrOwnership) {
t.Fatalf("Release() error = %v, want ErrOwnership", err)
}
if _, err := os.Stat(lock.Path()); err != nil {
t.Fatalf("foreign lock was removed: %v", err)
}
}