Commit 42e79860 authored by GuessWhoSamFoo's avatar GuessWhoSamFoo
Browse files

Fix sourcePath for imported json on root module


Signed-off-by: default avatarGuessWhoSamFoo <sfoohei@gmail.com>
parent f45eed14
......@@ -271,9 +271,6 @@ func (i *Import) createComponentFromData(name, data string, templateType prototy
func (i *Import) createComponent(fileName, base, ext string, templateType prototype.TemplateType) error {
var name bytes.Buffer
if i.module != "" {
name.WriteString(i.module + "/")
}
name.WriteString(strings.TrimSuffix(base, ext))
......
......@@ -76,7 +76,7 @@ func TestImport_http(t *testing.T) {
})
}
func TestImport_file(t *testing.T) {
func TestImport_yaml_file(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
dataPath := filepath.Join("testdata", "import", "file.yaml")
serviceData, err := ioutil.ReadFile(dataPath)
......@@ -111,6 +111,67 @@ func TestImport_file(t *testing.T) {
})
}
func TestImport_json_file(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
dataPath := filepath.Join("testdata", "import", "my-service.json")
serviceData, err := ioutil.ReadFile(dataPath)
require.NoError(t, err)
path := "/my-service.json"
stageFile(t, appMock.Fs(), "import/my-service.json", path)
cases := []struct {
name string
module string
path string
expectedModule string
}{
{
name: "root module",
module: "/",
expectedModule: "/",
},
{
name: "module",
module: "a",
expectedModule: "a",
},
{
name: "dot module",
module: "a.b",
expectedModule: "a.b",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
in := map[string]interface{}{
OptionApp: appMock,
OptionModule: tc.module,
OptionPath: path,
}
a, err := NewImport(in)
require.NoError(t, err)
a.createComponentFn = func(_ app.App, moduleName, name, text string, p params.Params, templateType prototype.TemplateType) (string, error) {
assert.Contains(t, name, "my-service")
assert.Equal(t, tc.expectedModule, moduleName)
assert.Equal(t, string(serviceData), text)
assert.Equal(t, params.Params{}, p)
assert.Equal(t, prototype.JSON, templateType)
return "/", nil
}
err = a.Run()
require.NoError(t, err)
})
}
})
}
func TestImport_directory(t *testing.T) {
withApp(t, func(appMock *amocks.App) {
dataPath := filepath.Join("testdata", "import", "file.yaml")
......
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "my-service"
},
"spec": {
"selector": {
"app": "MyApp"
},
"ports": [
{
"protocol": "TCP",
"port": 80,
"targetPort": 9376
}
]
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment