Skip to content

Commit 743bacf

Browse files
committed
feat: add duff test units
1 parent 0636f0f commit 743bacf

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

tests/diff_test.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package tests
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/vanilla-os/abroot/core"
8+
)
9+
10+
// TestMergeDiff tests the MergeDiff function by creating 2 files with
11+
// different content and merging them into a destination file. The destination
12+
// file should reflect the changes.
13+
func TestMergeDiff(t *testing.T) {
14+
firstFile := t.TempDir() + "/first"
15+
err := os.WriteFile(firstFile, []byte("first file"), 0644)
16+
if err != nil {
17+
t.Error(err)
18+
}
19+
20+
secondFile := t.TempDir() + "/second"
21+
err = os.WriteFile(secondFile, []byte("second file"), 0644)
22+
if err != nil {
23+
t.Error(err)
24+
}
25+
26+
destination := t.TempDir() + "/destination"
27+
28+
err = core.MergeDiff(firstFile, secondFile, destination)
29+
if err != nil {
30+
t.Error(err)
31+
}
32+
33+
firstFileContents, err := os.ReadFile(firstFile)
34+
if err != nil {
35+
t.Error(err)
36+
}
37+
38+
destinationContents, err := os.ReadFile(destination)
39+
if err != nil {
40+
t.Error(err)
41+
}
42+
43+
if string(firstFileContents) != string(destinationContents) {
44+
t.Error("destination file does not have the same content as the second file")
45+
}
46+
47+
t.Log("TestMergeDiff: destination file:", string(destinationContents))
48+
t.Log("TestMergeDiff: done")
49+
}
50+
51+
// TestDiffFiles tests the DiffFiles function in 2 cases:
52+
// 1. when the source and dest files have the same content
53+
// 2. when the source and dest files have different content
54+
func TestDiffFiles(t *testing.T) {
55+
// same content
56+
sourceFile := t.TempDir() + "/source"
57+
err := os.WriteFile(sourceFile, []byte("same file"), 0644)
58+
if err != nil {
59+
t.Error(err)
60+
}
61+
62+
destFile := t.TempDir() + "/dest"
63+
err = os.WriteFile(destFile, []byte("same file"), 0644)
64+
if err != nil {
65+
t.Error(err)
66+
}
67+
68+
diffLines, err := core.DiffFiles(sourceFile, destFile)
69+
if err != nil {
70+
t.Error(err)
71+
}
72+
73+
if diffLines != nil {
74+
t.Error("diff lines should be nil")
75+
}
76+
77+
t.Log("No diff found. Expected behavior.")
78+
79+
// different content
80+
err = os.WriteFile(destFile, []byte("different file"), 0644)
81+
if err != nil {
82+
t.Error(err)
83+
}
84+
85+
diffLines, err = core.DiffFiles(sourceFile, destFile)
86+
if err != nil {
87+
t.Error(err)
88+
}
89+
90+
if diffLines == nil {
91+
t.Error("diff lines should not be nil")
92+
}
93+
94+
t.Log("Diff: ", string(diffLines))
95+
t.Log("Diff found. Expected behavior.")
96+
}

0 commit comments

Comments
 (0)