@@ -16,8 +16,10 @@ package pkgmgr
16
16
17
17
import (
18
18
"reflect"
19
+ "runtime"
19
20
"strings"
20
21
"testing"
22
+ "text/template"
21
23
22
24
"gopkg.in/yaml.v3"
23
25
)
@@ -68,3 +70,64 @@ func TestPackageToYaml(t *testing.T) {
68
70
}
69
71
}
70
72
}
73
+
74
+ func TestOSAndARCH (t * testing.T ) {
75
+ expectOS := runtime .GOOS
76
+ expectARCH := runtime .GOARCH
77
+
78
+ // Initialized a config object
79
+ tempCacheDir := t .TempDir ()
80
+ tempDataDir := t .TempDir ()
81
+ cfg := Config {
82
+ CacheDir : tempCacheDir ,
83
+ DataDir : tempDataDir ,
84
+ BinDir : "/usr/local/bin" ,
85
+ Template : & Template {
86
+ tmpl : template .New ("test" ),
87
+ baseVars : make (map [string ]any ),
88
+ },
89
+ }
90
+
91
+ cfg .Template = cfg .Template .WithVars (
92
+ map [string ]any {
93
+ "System" : map [string ]string {
94
+ "OS" : runtime .GOOS ,
95
+ "ARCH" : runtime .GOARCH ,
96
+ },
97
+ },
98
+ )
99
+
100
+ // Defined a test package
101
+ pkg := Package {}
102
+ pkg .Name = "test-package"
103
+ pkg .Version = "1.0.0"
104
+ opts := make (map [string ]bool )
105
+
106
+ _ , _ , err := pkg .install (cfg , "test" , opts , false )
107
+ if err != nil {
108
+ t .Errorf ("Installation failed: %v" , err )
109
+ }
110
+
111
+ // Verify if OS and ARCH are injected into the config template
112
+ actualOS , err := cfg .Template .Render ("{{ .System.OS }}" , nil )
113
+ if err != nil {
114
+ t .Errorf ("Template rendering for OS failed: %v" , err )
115
+ } else if actualOS != expectOS {
116
+ t .Errorf ("Expected OS: %s and rendered OS: %s are not same" , expectOS , actualOS )
117
+ } else {
118
+ t .Logf ("Expected OS matched with rendered OS" )
119
+ }
120
+
121
+ actualARCH , err := cfg .Template .Render ("{{ .System.ARCH }}" , nil )
122
+ if err != nil {
123
+ t .Errorf ("Template rendering for ARCH failed: %v" , err )
124
+ } else if actualARCH != expectARCH {
125
+ t .Errorf ("Expected ARCH: %s and rendered ARCH: %s are not same" , expectARCH , actualARCH )
126
+ } else {
127
+ t .Logf ("Expected ARCH matched with rendered ARCH" )
128
+ }
129
+
130
+ if actualOS == expectOS && actualARCH == expectARCH {
131
+ t .Logf ("Test is successful and OS, ARCH values are correctly injected to config template" )
132
+ }
133
+ }
0 commit comments