aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/session_test.go
blob: ece69dd9f67a2e9e23d4b71039d4e6512ca1e77a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-License-Identifier: GPL-3.0-or-later

package engine

import (
	"context"
	"os"
	"path/filepath"
	"testing"
	"time"
)

// twoOverwritingDirs builds a and b, each holding x.pdf, both moving it to
// ~/Out with (on-conflict overwrite).
func twoOverwritingDirs(t *testing.T, h string) *Engine {
	t.Helper()
	old := time.Now().Add(-2 * time.Hour)
	dirs := map[string]string{}
	for _, n := range []string{"a", "b"} {
		p := filepath.Join(h, n, "x.pdf")
		if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
			t.Fatal(err)
		}
		if err := os.WriteFile(p, []byte("from "+n), 0o644); err != nil {
			t.Fatal(err)
		}
		os.Chtimes(p, old, old)
		dirs[n] = "(path \"~/" + n + "\")\n(on-conflict overwrite)\n(rule \"out\" (move \"~/Out\"))\n"
	}
	main := writeConfig(t, h, `(include "a" "b")`, dirs)
	e, errs := Load(main)
	if len(errs) > 0 {
		t.Fatal(errs)
	}
	return e
}

// TestSessionKeepsOnlyAppliedDestinationsClaimed: one session covers a whole
// run - its log, run id and claims - and after applying a directory only the
// paths its files ended up at stay claimed, so a later directory's overwrite
// takes a free name instead of trashing this run's own result (spec §7.4).
func TestSessionKeepsOnlyAppliedDestinationsClaimed(t *testing.T) {
	h := sandbox(t)
	e := twoOverwritingDirs(t, h)
	s, err := e.NewSession(false)
	if err != nil {
		t.Fatal(err)
	}
	defer s.Close()
	if s.Run() == "" {
		t.Error("a real session has no run id")
	}
	for _, d := range e.Dirs {
		dp, err := s.Plan(context.Background(), d)
		if err != nil {
			t.Fatal(err)
		}
		if _, err := s.Apply(context.Background(), dp, map[string]bool{"x.pdf": true}); err != nil {
			t.Fatal(err)
		}
	}
	for rel, want := range map[string]string{"Out/x.pdf": "from a", "Out/x_1.pdf": "from b"} {
		if b, err := os.ReadFile(filepath.Join(h, rel)); err != nil || string(b) != want {
			t.Errorf("%s: %q, %v; want %q", rel, b, err, want)
		}
	}
	if entries, _ := os.ReadDir(filepath.Join(h, ".local", "share", "Trash", "files")); len(entries) != 0 {
		t.Errorf("the Trash holds %d entries; nothing should have been displaced", len(entries))
	}
}

// TestDrySessionWritesNoLog: a dry session opens no log, so a dry run never
// creates the state directory's krino.log (spec §11).
func TestDrySessionWritesNoLog(t *testing.T) {
	h := sandbox(t)
	e := twoOverwritingDirs(t, h)
	s, err := e.NewSession(true)
	if err != nil {
		t.Fatal(err)
	}
	defer s.Close()
	if s.Run() != "" {
		t.Errorf("a dry session took a run id: %q", s.Run())
	}
	if _, err := s.Plan(context.Background(), e.Dirs[0]); err != nil {
		t.Fatal(err)
	}
	if _, err := os.Stat(e.Config.LogFile()); !os.IsNotExist(err) {
		t.Errorf("a dry session touched the log: %v", err)
	}
}

// TestSessionLockIsTheDirectorysOwn: the session takes the same lock a
// second krino waits for, and releasing it lets the next one in.
func TestSessionLockIsTheDirectorysOwn(t *testing.T) {
	h := sandbox(t)
	e := twoOverwritingDirs(t, h)
	s, err := e.NewSession(true)
	if err != nil {
		t.Fatal(err)
	}
	defer s.Close()
	l, err := s.Lock(context.Background(), e.Dirs[0], false)
	if err != nil {
		t.Fatal(err)
	}
	if _, err := s.Lock(context.Background(), e.Dirs[0], false); err == nil {
		t.Error("the directory was locked twice")
	}
	if err := l.Release(); err != nil {
		t.Fatal(err)
	}
	l2, err := s.Lock(context.Background(), e.Dirs[0], false)
	if err != nil {
		t.Errorf("the lock was not released: %v", err)
	} else {
		l2.Release()
	}
}