forked from zzet/gortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_test.go
More file actions
119 lines (98 loc) · 2.63 KB
/
Copy pathbash_test.go
File metadata and controls
119 lines (98 loc) · 2.63 KB
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
package languages
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zzet/gortex/internal/graph"
)
func TestBashExtractor_Functions(t *testing.T) {
src := []byte(`#!/bin/bash
function greet() {
echo "Hello $1"
}
deploy() {
echo "deploying..."
}
`)
e := NewBashExtractor()
result, err := e.Extract("deploy.sh", src)
require.NoError(t, err)
funcs := nodesOfKind(result.Nodes, graph.KindFunction)
require.Len(t, funcs, 2)
names := make(map[string]bool)
for _, f := range funcs {
names[f.Name] = true
assert.Equal(t, "bash", f.Language)
}
assert.True(t, names["greet"], "expected function 'greet'")
assert.True(t, names["deploy"], "expected function 'deploy'")
// Both should have EdgeDefines from the file node.
defines := edgesOfKind(result.Edges, graph.EdgeDefines)
funcDefines := 0
for _, e := range defines {
if e.From == "deploy.sh" {
funcDefines++
}
}
assert.GreaterOrEqual(t, funcDefines, 2)
}
func TestBashExtractor_Variables(t *testing.T) {
src := []byte(`#!/bin/bash
APP_NAME="myapp"
VERSION=1
LOCAL_VAR="hello"
my_func() {
INNER="should not be extracted"
}
`)
e := NewBashExtractor()
result, err := e.Extract("config.sh", src)
require.NoError(t, err)
vars := nodesOfKind(result.Nodes, graph.KindVariable)
require.Len(t, vars, 3, "expected 3 top-level variables")
names := make(map[string]bool)
for _, v := range vars {
names[v.Name] = true
}
assert.True(t, names["APP_NAME"])
assert.True(t, names["VERSION"])
assert.True(t, names["LOCAL_VAR"])
}
func TestBashExtractor_SourceImports(t *testing.T) {
src := []byte(`#!/bin/bash
source ./lib/utils.sh
. ./lib/helpers.sh
source config.sh
`)
e := NewBashExtractor()
result, err := e.Extract("main.sh", src)
require.NoError(t, err)
imports := edgesOfKind(result.Edges, graph.EdgeImports)
require.Len(t, imports, 3)
targets := make(map[string]bool)
for _, imp := range imports {
targets[imp.To] = true
}
assert.True(t, targets["unresolved::import::./lib/utils.sh"])
assert.True(t, targets["unresolved::import::./lib/helpers.sh"])
assert.True(t, targets["unresolved::import::config.sh"])
}
func TestBashExtractor_CallSites(t *testing.T) {
src := []byte(`#!/bin/bash
deploy() {
docker build .
kubectl apply -f deploy.yaml
}
`)
e := NewBashExtractor()
result, err := e.Extract("run.sh", src)
require.NoError(t, err)
calls := edgesOfKind(result.Edges, graph.EdgeCalls)
require.Len(t, calls, 2)
callNames := make(map[string]bool)
for _, c := range calls {
callNames[c.To] = true
}
assert.True(t, callNames["unresolved::docker"])
assert.True(t, callNames["unresolved::kubectl"])
}