1
+ package cmd
2
+
3
+ import (
4
+ "os/exec"
5
+ "strings"
6
+ "testing"
7
+ )
8
+
9
+ // Test validateAndGetRepoURL function
10
+ func TestValidateAndGetRepoURL (t * testing.T ) {
11
+ tests := []struct {
12
+ input string
13
+ expected string
14
+ }{
15
+ {"https://github.com/wilmoore/pro.git" , "https://github.com/wilmoore/pro.git" },
16
+ {"wilmoore/pro" , "https://github.com/wilmoore/pro.git" },
17
+ }
18
+
19
+ for _ , test := range tests {
20
+ result := validateAndGetRepoURL (test .input )
21
+ if result != test .expected {
22
+ t .Errorf ("validateAndGetRepoURL(%q) = %q; want %q" , test .input , result , test .expected )
23
+ }
24
+ }
25
+ }
26
+
27
+ // Test processTags function
28
+ func TestProcessTags (t * testing.T ) {
29
+ tests := []struct {
30
+ providerTag string
31
+ cliTags string
32
+ expected string
33
+ }{
34
+ {"digitalocean" , "web,db" , "pro,digitalocean,web,db" },
35
+ {"aws" , "" , "pro,aws" },
36
+ {"gcp" , "backend,api" , "pro,gcp,backend,api" },
37
+ }
38
+
39
+ for _ , test := range tests {
40
+ result := processTags (test .providerTag , test .cliTags )
41
+ if result != test .expected {
42
+ t .Errorf ("processTags(%q, %q) = %q; want %q" , test .providerTag , test .cliTags , result , test .expected )
43
+ }
44
+ }
45
+ }
46
+
47
+ // Test generateCloudInit function
48
+ func TestGenerateCloudInit (t * testing.T ) {
49
+ repoURL := "https://github.com/wilmoore/pro.git"
50
+ branch := "main"
51
+ playbookPath := "src/pro"
52
+
53
+ result := generateCloudInit (repoURL , branch , playbookPath )
54
+
55
+ expectedParts := []string {
56
+ "git clone -b main https://github.com/wilmoore/pro.git /opt/ansible" ,
57
+ "cd /opt/ansible/src/pro" ,
58
+ "ansible-playbook -i \" localhost,\" -c local playbook.yml" ,
59
+ }
60
+
61
+ for _ , part := range expectedParts {
62
+ if ! strings .Contains (result , part ) {
63
+ t .Errorf ("generateCloudInit() missing expected content: %q" , part )
64
+ }
65
+ }
66
+ }
67
+
68
+ // Test getSSHKeys function
69
+ func TestGetSSHKeys (t * testing.T ) {
70
+ // Mock doctl response
71
+ mockOutput := "123456\n 789012\n "
72
+ cmd := exec .Command ("bash" , "-c" , "echo -n '" + mockOutput + "'" ) // Simulate doctl output
73
+
74
+ output , err := cmd .Output ()
75
+ if err != nil {
76
+ t .Fatalf ("Error executing mock command: %v" , err )
77
+ }
78
+
79
+ expected := "123456,789012"
80
+ result := strings .TrimRight (strings .TrimSpace (strings .ReplaceAll (string (output ), "\n " , "," )), "," )
81
+ if result != expected {
82
+ t .Errorf ("getSSHKeys() = %q; want %q" , result , expected )
83
+ }
84
+ }
85
+
86
+ // Test runCommand function
87
+ func TestRunCommand (t * testing.T ) {
88
+ output , err := runCommand ("echo" , "hello" )
89
+ if err != nil {
90
+ t .Fatalf ("runCommand() failed: %v" , err )
91
+ }
92
+ expected := "hello\n "
93
+ if output != expected {
94
+ t .Errorf ("runCommand() = %q; want %q" , output , expected )
95
+ }
96
+ }
97
+
98
+ // Test fzfSelect function (mocked)
99
+ func TestFzfSelect (t * testing.T ) {
100
+ mockOptions := []string {"Option 1" , "Option 2" , "Option 3" }
101
+
102
+ // Mock fzf by using `echo` to select the first option
103
+ cmd := exec .Command ("bash" , "-c" , "echo 'Option 1'" )
104
+ cmd .Stdin = strings .NewReader (strings .Join (mockOptions , "\n " ))
105
+
106
+ output , err := cmd .Output ()
107
+ if err != nil {
108
+ t .Fatalf ("fzfSelect() mock failed: %v" , err )
109
+ }
110
+
111
+ expected := "Option 1"
112
+ result := strings .TrimSpace (string (output ))
113
+ if result != expected {
114
+ t .Errorf ("fzfSelect() = %q; want %q" , result , expected )
115
+ }
116
+ }
0 commit comments