This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyzer_test.go
46 lines (42 loc) · 1.39 KB
/
analyzer_test.go
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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"testing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
)
func TestCompare(t *testing.T) {
repo := Repository{
Addr: "https://github.com/leitosama/SubSigma",
Branch: "dev",
LastCommit: "7f81f8f99065340abcdb7ed5f8dc825113ad739c",
RulesPath: "test/data/rules/",
}
rulefile := "test/data/rules/example.yml"
objrepo, _ := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: repo.Addr,
})
beforenewfile, _ := objrepo.CommitObject(plumbing.NewHash(repo.LastCommit))
ref, _ := objrepo.Reference(plumbing.ReferenceName(fmt.Sprintf("refs/remotes/origin/%s", repo.Branch)), false)
hCommit, _ := objrepo.CommitObject(ref.Hash())
got, _ := Compare(hCommit, beforenewfile, &repo)
rurl := fmt.Sprintf("%s/blob/%s/%s", repo.Addr, repo.Branch, rulefile)
want := []FileChange{{ChangeType: 0, BaseName: filepath.Base(rulefile), RemoteUrl: rurl, Path: rulefile}}
if got[0] != want[0] {
t.Errorf("\ngot: %v\nwant: %v", got, want)
}
}
func TestEnrichFileChange(t *testing.T) {
example := FileChange{Path: "test/data/rules/example.yml"}
d, _ := ioutil.ReadFile(example.Path)
want := example
want.RuleDesc = "Sample description"
want.RuleTitle = "Test rule"
got := EnrichFileChange(example, d)
if got != want {
t.Errorf("\ngot: %v\nwant: %v", got, want)
}
}