This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
/
bal_executor.go
140 lines (124 loc) · 4.68 KB
/
bal_executor.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
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
/*
* Copyright (c) 2019 WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you 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 test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"cellery.io/cellery/components/cli/pkg/ballerina"
)
type MockBalExecutor struct {
currentDir string
version string
executablePath string
yamlName string
yamlContent []byte
metadataJsonContent []byte
referenceJsonContent []byte
}
// NewMockBalExecutor returns a MockBalExecutor instance.
func NewMockBalExecutor(opts ...func(*MockBalExecutor)) *MockBalExecutor {
balExecutor := &MockBalExecutor{
executablePath: "",
}
for _, opt := range opts {
opt(balExecutor)
}
return balExecutor
}
func SetBalVersion(version string) func(*MockBalExecutor) {
return func(balExecutor *MockBalExecutor) {
balExecutor.version = version
}
}
func SetBalCurrentDir(currentDir string) func(*MockBalExecutor) {
return func(balExecutor *MockBalExecutor) {
balExecutor.currentDir = currentDir
}
}
func SetYamlName(name string) func(*MockBalExecutor) {
return func(balExecutor *MockBalExecutor) {
balExecutor.yamlName = name
}
}
func SetYamlContent(content []byte) func(*MockBalExecutor) {
return func(balExecutor *MockBalExecutor) {
balExecutor.yamlContent = content
}
}
func SetMetadataJsonContent(content []byte) func(*MockBalExecutor) {
return func(balExecutor *MockBalExecutor) {
balExecutor.metadataJsonContent = content
}
}
func SetReferenceJsonContent(content []byte) func(*MockBalExecutor) {
return func(balExecutor *MockBalExecutor) {
balExecutor.referenceJsonContent = content
}
}
// Build mocks execution of ballerina build on an executable bal file.
func (balExecutor *MockBalExecutor) Build(fileName string, args []string, cmdDir string) error {
var err error
var metadataJson, yaml, referenceJson *os.File
if err := os.MkdirAll(filepath.Join(cmdDir, "target", "cellery"), os.ModePerm); err != nil {
return fmt.Errorf("failed to create target/cellery dir, %v", err)
}
if metadataJson, err = os.Create(filepath.Join(cmdDir, "target", "cellery", "metadata.json")); err != nil {
return fmt.Errorf("failed to create metadata.json, %v", err)
}
if err := ioutil.WriteFile(metadataJson.Name(), []byte(balExecutor.metadataJsonContent), os.ModePerm); err != nil {
return fmt.Errorf("failed to write to metadata.json, %v", err)
}
if yaml, err = os.Create(filepath.Join(cmdDir, "target", "cellery", balExecutor.yamlName)); err != nil {
return fmt.Errorf("failed to create %s, %v", balExecutor.yamlName, err)
}
if err := ioutil.WriteFile(yaml.Name(), []byte(balExecutor.yamlContent), os.ModePerm); err != nil {
return fmt.Errorf("failed to write to yaml, %v", err)
}
if err := os.MkdirAll(filepath.Join(cmdDir, "target", "ref"), os.ModePerm); err != nil {
return fmt.Errorf("failed to create target/cellery dir, %v", err)
}
if referenceJson, err = os.Create(filepath.Join(cmdDir, "target", "ref", "reference.json")); err != nil {
return fmt.Errorf("failed to create metadata.json, %v", err)
}
if err := ioutil.WriteFile(referenceJson.Name(), []byte(balExecutor.referenceJsonContent), os.ModePerm); err != nil {
return fmt.Errorf("failed to write to reference .json, %v", err)
}
return nil
}
// Build mocks execution of ballerina run on an executable bal file.
func (balExecutor *MockBalExecutor) Run(fileName string, args []string, envVars []*ballerina.EnvironmentVariable, cmdDir string) error {
return nil
}
// Build mocks execution of ballerina run for tests on an executable bal file.
func (balExecutor *MockBalExecutor) Test(args []string, envVars []*ballerina.EnvironmentVariable, cmdDir string) error {
return nil
}
// Build mocks execution of ballerina run for cellery init on an executable bal file.
func (balExecutor *MockBalExecutor) Init(workingDir, projectName, moduleName string) error {
return nil
}
// Version returns the mock ballerina version.
func (balExecutor *MockBalExecutor) Version() (string, error) {
return balExecutor.version, nil
}
// ExecutablePath returns mock ballerina executable path.
func (balExecutor *MockBalExecutor) ExecutablePath() (string, error) {
return balExecutor.executablePath, nil
}