Skip to content

Commit 822ab78

Browse files
committed
[#86285] devices: linux-client: Add script tests
Signed-off-by: Anna Roszkiewicz <aroszkiewicz@antmicro.com>
1 parent 55fa928 commit 822ab78

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package app
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/sirupsen/logrus"
9+
"github.com/sirupsen/logrus/hooks/test"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestCommitScriptSuccess(t *testing.T) {
14+
filePath := filepath.Join(os.TempDir(), "valid-script.sh")
15+
file, _ := os.Create(filePath)
16+
17+
content := []byte("#!/bin/bash \n echo valid \n exit 0")
18+
os.WriteFile(filePath, content, 0755)
19+
os.Chmod(filePath, 0755)
20+
file.Close()
21+
22+
hook := test.NewGlobal()
23+
24+
err := checkBeforeCommit(filePath)
25+
26+
assert.NoError(t, err)
27+
assert.Equal(t, 2, len(hook.Entries))
28+
assert.Equal(t, "System correctness successfully verified", hook.Entries[0].Message)
29+
assert.Equal(t, "valid\n", hook.Entries[1].Message)
30+
assert.Equal(t, logrus.InfoLevel, hook.Entries[0].Level)
31+
assert.Equal(t, logrus.InfoLevel, hook.Entries[1].Level)
32+
33+
os.Remove(filePath)
34+
}
35+
36+
func TestCommitScriptFailure(t *testing.T) {
37+
filePath := filepath.Join(os.TempDir(), "invalid-script.sh")
38+
file, _ := os.Create(filePath)
39+
40+
content := []byte("#!/bin/bash \n echo invalid \n >&2 echo error \n exit 1")
41+
os.WriteFile(filePath, content, 0755)
42+
os.Chmod(filePath, 0755)
43+
file.Close()
44+
45+
hook := test.NewGlobal()
46+
47+
err := checkBeforeCommit(filePath)
48+
49+
assert.Error(t, err)
50+
assert.Equal(t, 2, len(hook.Entries))
51+
assert.Equal(t, "System correctness verification failed", hook.Entries[0].Message)
52+
assert.Equal(t, "invalid\nerror\n", hook.Entries[1].Message)
53+
assert.Equal(t, logrus.ErrorLevel, hook.Entries[0].Level)
54+
assert.Equal(t, logrus.ErrorLevel, hook.Entries[1].Level)
55+
56+
os.Remove(filePath)
57+
}
58+
59+
func TestCommitScriptNotSet(t *testing.T) {
60+
err := checkBeforeCommit("")
61+
assert.NoError(t, err)
62+
}
63+
64+
func TestCommitScriptNotFound(t *testing.T) {
65+
err := checkBeforeCommit("/this/path/is/invalid")
66+
assert.ErrorIs(t, err, os.ErrNotExist)
67+
}

0 commit comments

Comments
 (0)