-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathruntime_test.go
More file actions
146 lines (135 loc) · 4.14 KB
/
runtime_test.go
File metadata and controls
146 lines (135 loc) · 4.14 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2022-2026 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package runtime
import (
"path/filepath"
"testing"
"github.com/slackapi/slack-cli/internal/hooks"
"github.com/slackapi/slack-cli/internal/runtime/deno"
"github.com/slackapi/slack-cli/internal/runtime/node"
"github.com/slackapi/slack-cli/internal/runtime/python"
"github.com/slackapi/slack-cli/internal/slackcontext"
"github.com/slackapi/slack-cli/internal/slackdeps"
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func Test_Runtime_New(t *testing.T) {
tests := map[string]struct {
runtime string
expectedRuntimeType Runtime
}{
"Deno SDK": {
runtime: "deno",
expectedRuntimeType: deno.New(),
},
"Bolt for JavaScript": {
runtime: "node",
expectedRuntimeType: node.New(),
},
"Bolt for Python": {
runtime: "python",
expectedRuntimeType: python.New(),
},
"Unsupported Runtime": {
runtime: "biggly-boo",
expectedRuntimeType: nil,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Run the test
rt, _ := New(tc.runtime)
require.IsType(t, tc.expectedRuntimeType, rt)
})
}
}
func Test_ActivatePythonVenvIfPresent(t *testing.T) {
tests := map[string]struct {
createVenv bool
expectedActivated bool
}{
"activates venv when it exists": {
createVenv: true,
expectedActivated: true,
},
"no-op when venv does not exist": {
createVenv: false,
expectedActivated: false,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
fs := slackdeps.NewFsMock()
osMock := slackdeps.NewOsMock()
projectDir := "/path/to/project"
venvPath := filepath.Join(projectDir, ".venv")
osMock.On("Getenv", "PATH").Return("/usr/bin:/bin")
osMock.AddDefaultMocks()
if tc.createVenv {
// Create the pip executable so venvExists returns true
pipPath := filepath.Join(venvPath, "bin", "pip")
err := fs.MkdirAll(filepath.Dir(pipPath), 0755)
require.NoError(t, err)
err = afero.WriteFile(fs, pipPath, []byte(""), 0755)
require.NoError(t, err)
}
activated, err := ActivatePythonVenvIfPresent(fs, osMock, projectDir)
require.NoError(t, err)
require.Equal(t, tc.expectedActivated, activated)
if tc.expectedActivated {
osMock.AssertCalled(t, "Setenv", "VIRTUAL_ENV", venvPath)
osMock.AssertCalled(t, "Setenv", "PATH", mock.Anything)
osMock.AssertCalled(t, "Unsetenv", "PYTHONHOME")
} else {
osMock.AssertNotCalled(t, "Setenv", mock.Anything, mock.Anything)
osMock.AssertNotCalled(t, "Unsetenv", mock.Anything)
}
})
}
}
func Test_Runtime_NewDetectProject(t *testing.T) {
tests := map[string]struct {
sdkConfig hooks.SDKCLIConfig
expectedRuntimeType Runtime
}{
"Deno SDK": {
sdkConfig: hooks.SDKCLIConfig{Runtime: "deno"},
expectedRuntimeType: deno.New(),
},
"Bolt for JavaScript": {
sdkConfig: hooks.SDKCLIConfig{Runtime: "node"},
expectedRuntimeType: node.New(),
},
"Bolt for Python": {
sdkConfig: hooks.SDKCLIConfig{Runtime: "python"},
expectedRuntimeType: python.New(),
},
"Unsupported Runtime": {
sdkConfig: hooks.SDKCLIConfig{Runtime: ""},
expectedRuntimeType: nil,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// Setup
ctx := slackcontext.MockContext(t.Context())
fs := afero.NewMemMapFs()
projectDirPath := "/path/to/project-name"
// Run the test
rt, _ := NewDetectProject(ctx, fs, projectDirPath, tc.sdkConfig)
require.IsType(t, tc.expectedRuntimeType, rt)
})
}
}