package dll_test
import (
"os"
"path/filepath"
"testing"
"git.sr.ht/~hristoast/wem/cfg"
"git.sr.ht/~hristoast/wem/dll"
)
func TestDll(t *testing.T) {
// Setup
dir := t.TempDir()
os.Setenv("HOME", dir)
config, _ := cfg.GetConfig()
dllFile := "wemTest.dll"
targetVer := "1.2.3"
targetCachePath := filepath.Join(config.CacheDir, "test"+"-"+targetVer)
testPrefix := filepath.Join(os.Getenv("HOME"), "WinePrefix")
targetDstDir32 := filepath.Join(testPrefix, "drive_c", "windows", "system32")
targetDstDir64 := filepath.Join(testPrefix, "drive_c", "windows", "syswow64")
targetDstPath32 := filepath.Join(targetDstDir32, dllFile)
targetDstPath64 := filepath.Join(targetDstDir64, dllFile)
targetBakPath32 := filepath.Join(targetDstDir32, dllFile+".orig-wem")
targetBakPath64 := filepath.Join(targetDstDir64, dllFile+".orig-wem")
targetSrcPath32 := filepath.Join(targetCachePath, "test"+"-"+targetVer, "x64", dllFile)
targetSrcPath64 := filepath.Join(targetCachePath, "test"+"-"+targetVer, "x86", dllFile)
// Create fake initial dirs
err := os.MkdirAll(targetCachePath, 0755)
if err != nil {
t.Error(err)
}
err = os.MkdirAll(targetDstDir32, 0755)
if err != nil {
t.Error(err)
}
err = os.MkdirAll(targetDstDir64, 0755)
if err != nil {
t.Error(err)
}
// Create fake initial files
for _, file := range []string{targetDstPath32, targetDstPath64} {
f, err := os.Create(file)
if err != nil {
t.Error(err)
}
err = f.Close()
if err != nil {
t.Error(err)
}
}
has64 := true
wineArch := "win64"
err = dll.EnableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
if err != nil {
t.Errorf("Got an error enabling: %v\n", err)
}
// Enabled tests
t.Run("32-bit backup file should be a file", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath32)
if !isFile {
t.Error("got: file doesn't exist; want: file exists")
}
})
t.Run("64-bit backup file should be a file", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath64)
if !isFile {
t.Error("got: file doesn't exist; want: file exists")
}
})
t.Run("32-bit file should be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath32)
if !isSym {
t.Error("got: file isn't a symlink; want: file is a symlink")
}
})
t.Run("64-bit file should be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath64)
if !isSym {
t.Error("got: file isn't a symlink; want: file is a symlink")
}
})
t.Run("32-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
valid := dll.ValidLink(targetDstPath32, targetSrcPath32)
if !valid {
t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
}
})
t.Run("64-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
valid := dll.ValidLink(targetDstPath64, targetSrcPath64)
if !valid {
t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
}
})
err = dll.DisableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
if err != nil {
t.Errorf("Got an error disabling: %v\n", err)
}
// Disabled tests
t.Run("32-bit backup file should not exist", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath32)
if isFile {
t.Error("got: the backup file exists; want: the backup file doesn't exist")
}
})
t.Run("64-bit backup file should not exist", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath64)
if isFile {
t.Error("got: the backup file exists; want: the backup file doesn't exist")
}
})
t.Run("32-bit file should not be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath32)
if isSym {
t.Error("got: file is a symlink; want: file isn't a symlink")
}
})
t.Run("64-bit file should not be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath64)
if isSym {
t.Error("got: file is a symlink; want: file isn't a symlink")
}
})
// Erasing the backup files shouldn't cause any issues;
// re-enable, delete the backup files, then re-disable to confirm this.
err = dll.EnableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
if err != nil {
t.Errorf("Got an error enabling: %v\n", err)
}
err = os.Remove(targetBakPath32)
if err != nil {
t.Errorf("Got an error removing the targetBakPath32: %v\n", err)
}
err = os.Remove(targetBakPath64)
if err != nil {
t.Errorf("Got an error removing the targetBakPath64: %v\n", err)
}
// Disabling with the backup files removed should not cause problems
err = dll.DisableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
if err != nil {
t.Errorf("Got an error disabling: %v\n", err)
}
// Disabled tests
t.Run("32-bit backup file should not exist", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath32)
if isFile {
t.Error("got: the backup file exists; want: the backup file doesn't exist")
}
})
t.Run("64-bit backup file should not exist", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath64)
if isFile {
t.Error("got: the backup file exists; want: the backup file doesn't exist")
}
})
t.Run("32-bit file should not be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath32)
if isSym {
t.Error("got: file is a symlink; want: file isn't a symlink")
}
})
t.Run("64-bit file should not be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath64)
if isSym {
t.Error("got: file is a symlink; want: file isn't a symlink")
}
})
// Enabling/disabling when the original files don't exist should be fine as well.
// Delete the original files, then re-enable/re-disable to confirm this.
err = dll.EnableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
if err != nil {
t.Errorf("Got an error enabling: %v\n", err)
}
t.Run("32-bit file should be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath32)
if !isSym {
t.Error("got: file isn't a symlink; want: file is a symlink")
}
})
t.Run("64-bit file should be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath64)
if !isSym {
t.Error("got: file isn't a symlink; want: file is a symlink")
}
})
t.Run("32-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
valid := dll.ValidLink(targetDstPath32, targetSrcPath32)
if !valid {
t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
}
})
t.Run("64-bit file should be a valid symlink that points to the right file", func(t *testing.T) {
valid := dll.ValidLink(targetDstPath64, targetSrcPath64)
if !valid {
t.Error("got: symlink doesn't point to the right file; want: symlink points to the right file")
}
})
err = dll.DisableAll(has64, targetCachePath, "test", wineArch, testPrefix, targetVer)
if err != nil {
t.Errorf("Got an error disabling: %v\n", err)
}
// Disabled tests
t.Run("32-bit backup file should not exist", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath32)
if isFile {
t.Error("got: the backup file exists; want: the backup file doesn't exist")
}
})
t.Run("64-bit backup file should not exist", func(t *testing.T) {
isFile := dll.IsFile(targetBakPath64)
if isFile {
t.Error("got: the backup file exists; want: the backup file doesn't exist")
}
})
t.Run("32-bit file should not be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath32)
if isSym {
t.Error("got: file is a symlink; want: file isn't a symlink")
}
})
t.Run("64-bit file should not be a symlink", func(t *testing.T) {
isSym := dll.IsSymlink(targetDstPath64)
if isSym {
t.Error("got: file is a symlink; want: file isn't a symlink")
}
})
os.RemoveAll(dir)
}