f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package sync
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func createTempFile(t *testing.T, content []byte) string {
|
|
t.Helper()
|
|
cleanName := strings.ReplaceAll(t.Name(), "/", "_")
|
|
path := filepath.Join(t.TempDir(), cleanName+".txt")
|
|
require.NoError(t, os.WriteFile(path, content, 0o644))
|
|
return path
|
|
}
|
|
|
|
func TestComputeHash(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want string
|
|
}{
|
|
{
|
|
name: "hello world",
|
|
input: "hello world\n",
|
|
want: helloWorldHash,
|
|
},
|
|
{
|
|
name: "empty input",
|
|
input: "",
|
|
want: emptyInputHash,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ComputeHash(strings.NewReader(tt.input))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestComputeFileHash(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
setup func(t *testing.T) string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "hello world",
|
|
setup: func(t *testing.T) string {
|
|
return createTempFile(t, []byte("hello world\n"))
|
|
},
|
|
want: helloWorldHash,
|
|
},
|
|
{
|
|
name: "empty file",
|
|
setup: func(t *testing.T) string {
|
|
return createTempFile(t, []byte(""))
|
|
},
|
|
want: emptyInputHash,
|
|
},
|
|
{
|
|
name: "missing file",
|
|
setup: func(t *testing.T) string {
|
|
return filepath.Join(t.TempDir(), "nonexistent.txt")
|
|
},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
path := tt.setup(t)
|
|
|
|
got, err := ComputeFileHash(path)
|
|
if tt.wantErr {
|
|
require.Error(t, err)
|
|
requirePathError(t, err)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestComputeHash_ReaderError(t *testing.T) {
|
|
errInjected := errors.New("injected error")
|
|
reader := &failingReader{err: errInjected}
|
|
_, err := ComputeHash(reader)
|
|
require.Error(t, err)
|
|
require.ErrorIs(t, err, errInjected)
|
|
}
|
|
|
|
func TestComputeFileHash_ReadError(t *testing.T) {
|
|
// Use a directory to simulate a read error after open
|
|
dir := t.TempDir()
|
|
_, err := ComputeFileHash(dir)
|
|
require.Error(t, err)
|
|
// On most systems, reading a directory fails.
|
|
requirePathError(t, err)
|
|
}
|