-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package templates | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/azure/azure-dev/cli/azd/pkg/config" | ||
"github.com/azure/azure-dev/cli/azd/pkg/osutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_NewFileTemplateSource_FileExists(t *testing.T) { | ||
name := "test" | ||
configDir, err := config.GetUserConfigDir() | ||
require.NoError(t, err) | ||
|
||
path := filepath.Join(configDir, "test-templates.json") | ||
jsonTemplates := `[{"name": "template1", "repositoryPath": "path/to/template1"}, {"name": "template2", "repositoryPath": "path/to/template2"}]` | ||
err = os.WriteFile(path, []byte(jsonTemplates), osutil.PermissionFile) | ||
require.Nil(t, err) | ||
|
||
source, err := NewFileTemplateSource(name, path) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, name, source.Name()) | ||
|
||
err = os.Remove(path) | ||
require.Nil(t, err) | ||
} | ||
|
||
func Test_NewFileTemplateSource_InvalidJson(t *testing.T) { | ||
name := "test" | ||
configDir, err := config.GetUserConfigDir() | ||
require.NoError(t, err) | ||
|
||
path := filepath.Join(configDir, "test-templates.json") | ||
invalidJson := `invalid json` | ||
err = os.WriteFile(path, []byte(invalidJson), osutil.PermissionFile) | ||
require.Nil(t, err) | ||
|
||
_, err = NewFileTemplateSource(name, path) | ||
require.Error(t, err) | ||
|
||
err = os.Remove(path) | ||
require.Nil(t, err) | ||
} | ||
|
||
func Test_NewFileTemplateSource_FileDoesNotExist(t *testing.T) { | ||
name := "test" | ||
path := "testdata/nonexistent.json" | ||
_, err := NewFileTemplateSource(name, path) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package templates | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var testTemplates []*Template = []*Template{ | ||
{ | ||
Name: "template1", | ||
Description: "Description of template 1", | ||
RepositoryPath: "path/to/template1", | ||
}, | ||
{ | ||
Name: "template2", | ||
Description: "Description of template 2", | ||
RepositoryPath: "path/to/template2", | ||
}, | ||
} | ||
|
||
func jsonTemplates() string { | ||
jsonTemplates, err := json.Marshal(testTemplates) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return string(jsonTemplates) | ||
} | ||
|
||
func Test_NewJsonTemplateSource(t *testing.T) { | ||
name := "test" | ||
source, err := NewJsonTemplateSource(name, jsonTemplates()) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, name, source.Name()) | ||
} | ||
|
||
func Test_NewJsonTemplateSource_InvalidJson(t *testing.T) { | ||
name := "test" | ||
jsonTemplates := `invalid json` | ||
_, err := NewJsonTemplateSource(name, jsonTemplates) | ||
require.Error(t, err) | ||
} | ||
|
||
func Test_JsonTemplateSource_ListTemplates(t *testing.T) { | ||
name := "test" | ||
source, err := NewJsonTemplateSource(name, jsonTemplates()) | ||
require.Nil(t, err) | ||
|
||
templates, err := source.ListTemplates(context.Background()) | ||
require.Nil(t, err) | ||
require.Equal(t, 2, len(templates)) | ||
|
||
expectedTemplate1 := &Template{Name: "template1", RepositoryPath: "path/to/template1", Source: name} | ||
require.Equal(t, expectedTemplate1, templates[0]) | ||
|
||
expectedTemplate2 := &Template{Name: "template2", RepositoryPath: "path/to/template2", Source: name} | ||
require.Equal(t, expectedTemplate2, templates[1]) | ||
} | ||
|
||
func Test_JsonTemplateSource_GetTemplate_MatchFound(t *testing.T) { | ||
name := "test" | ||
source, err := NewJsonTemplateSource(name, jsonTemplates()) | ||
require.Nil(t, err) | ||
|
||
template, err := source.GetTemplate(context.Background(), "path/to/template1") | ||
require.Nil(t, err) | ||
|
||
expectedTemplate := &Template{Name: "template1", RepositoryPath: "path/to/template1", Source: name} | ||
require.Equal(t, expectedTemplate, template) | ||
} | ||
|
||
func Test_JsonTemplateSource_GetTemplate_NoMatchFound(t *testing.T) { | ||
name := "test" | ||
source, err := NewJsonTemplateSource(name, jsonTemplates()) | ||
require.Nil(t, err) | ||
|
||
template, err := source.GetTemplate(context.Background(), "path/to/template3") | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, ErrTemplateNotFound) | ||
|
||
require.Nil(t, template) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package templates | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/azure/azure-dev/cli/azd/pkg/config" | ||
"github.com/azure/azure-dev/cli/azd/test/mocks" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_sourceManager_List(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
config := config.NewConfig(nil) | ||
config.Set("template.sources", map[string]interface{}{ | ||
"test": map[string]interface{}{ | ||
"type": "file", | ||
"location": "testdata/templates.json", | ||
}, | ||
}) | ||
configManager.On("Load").Return(config, nil) | ||
|
||
sources, err := sm.List(context.Background()) | ||
require.Nil(t, err) | ||
|
||
require.Len(t, sources, 1) | ||
require.Equal(t, "test", sources[0].Key) | ||
} | ||
|
||
func Test_sourceManager_Get(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
config := config.NewConfig(nil) | ||
config.Set("template.sources", map[string]interface{}{ | ||
"test": map[string]interface{}{ | ||
"type": "file", | ||
"location": "testdata/templates.json", | ||
}, | ||
}) | ||
configManager.On("Load").Return(config, nil) | ||
|
||
source, err := sm.Get(context.Background(), "test") | ||
require.Nil(t, err) | ||
require.NotNil(t, source) | ||
|
||
require.Equal(t, "test", source.Key) | ||
} | ||
|
||
func Test_sourceManager_Add(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
config := config.NewConfig(nil) | ||
config.Set("template.sources", map[string]interface{}{}) | ||
configManager.On("Load").Return(config, nil) | ||
configManager.On("Save", mock.Anything).Return(nil) | ||
|
||
key := "test" | ||
source := &SourceConfig{ | ||
Type: SourceFile, | ||
Location: "testdata/templates.json", | ||
} | ||
err := sm.Add(context.Background(), key, source) | ||
require.Nil(t, err) | ||
} | ||
|
||
func Test_sourceManager_Remove(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
key := "test" | ||
config := config.NewConfig(nil) | ||
config.Set("template.sources.test", map[string]interface{}{}) | ||
configManager.On("Load").Return(config, nil) | ||
configManager.On("Save", mock.Anything).Return(nil) | ||
|
||
err := sm.Remove(context.Background(), key) | ||
require.Nil(t, err) | ||
} | ||
|
||
func Test_sourceManager_CreateSource_InvalidType(t *testing.T) { | ||
config := &SourceConfig{ | ||
Name: "test", | ||
Type: "invalid", | ||
} | ||
|
||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
_, err := sm.CreateSource(context.Background(), config) | ||
require.NotNil(t, err) | ||
} | ||
|
||
func Test_sourceManager_CreateSource_InvalidLocation(t *testing.T) { | ||
config := &SourceConfig{ | ||
Name: "test", | ||
Type: SourceFile, | ||
Location: "invalid", | ||
} | ||
|
||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
_, err := sm.CreateSource(context.Background(), config) | ||
require.NotNil(t, err) | ||
} | ||
|
||
func Test_sourceManager_CreateSource_File(t *testing.T) { | ||
config := &SourceConfig{ | ||
Name: "test", | ||
Type: SourceFile, | ||
Location: "testdata/templates.json", | ||
} | ||
|
||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
source, err := sm.CreateSource(context.Background(), config) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, config.Name, source.Name()) | ||
} | ||
|
||
func Test_sourceManager_CreateSource_Url(t *testing.T) { | ||
config := &SourceConfig{ | ||
Name: "test", | ||
Type: SourceUrl, | ||
Location: "https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore", | ||
} | ||
|
||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
source, err := sm.CreateSource(context.Background(), config) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, config.Name, source.Name()) | ||
} | ||
|
||
func Test_sourceManager_CreateSource_Resource(t *testing.T) { | ||
config := &SourceConfig{ | ||
Name: "test", | ||
Type: SourceResource, | ||
Location: "", | ||
} | ||
|
||
mockContext := mocks.NewMockContext(context.Background()) | ||
configManager := &mockUserConfigManager{} | ||
sm := NewSourceManager(configManager, mockContext.HttpClient) | ||
|
||
source, err := sm.CreateSource(context.Background(), config) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, config.Name, source.Name()) | ||
} | ||
|
||
type mockUserConfigManager struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *mockUserConfigManager) Load() (config.Config, error) { | ||
args := m.Called() | ||
return args.Get(0).(config.Config), args.Error(1) | ||
} | ||
|
||
func (m *mockUserConfigManager) Save(config config.Config) error { | ||
args := m.Called(config) | ||
return args.Error(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package templates | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/azure/azure-dev/cli/azd/test/mocks" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_NewUrlTemplateSource_ValidUrl(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
|
||
name := "test" | ||
url := "https://example.com/templates.json" | ||
|
||
mockContext.HttpClient.When(func(req *http.Request) bool { | ||
return req.Method == http.MethodGet && req.URL.String() == url | ||
}).RespondFn(func(req *http.Request) (*http.Response, error) { | ||
return mocks.CreateHttpResponseWithBody(req, http.StatusOK, testTemplates) | ||
}) | ||
|
||
source, err := NewUrlTemplateSource(context.Background(), name, url, mockContext.HttpClient) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, name, source.Name()) | ||
} | ||
|
||
func Test_NewUrlTemplateSource_ValidUrl_InvalidJson(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
|
||
name := "test" | ||
url := "https://example.com/templates.json" | ||
|
||
mockContext.HttpClient.When(func(req *http.Request) bool { | ||
return req.Method == http.MethodGet && req.URL.String() == url | ||
}).RespondFn(func(req *http.Request) (*http.Response, error) { | ||
return mocks.CreateHttpResponseWithBody(req, http.StatusOK, "invalid json") | ||
}) | ||
|
||
source, err := NewUrlTemplateSource(context.Background(), name, url, mockContext.HttpClient) | ||
require.Nil(t, source) | ||
require.Error(t, err) | ||
} | ||
|
||
func Test_NewUrlTemplateSource_InvalidUrl(t *testing.T) { | ||
mockContext := mocks.NewMockContext(context.Background()) | ||
|
||
name := "test" | ||
url := "https://example.com/templates.json" | ||
|
||
mockContext.HttpClient.When(func(req *http.Request) bool { | ||
return req.Method == http.MethodGet && req.URL.String() == url | ||
}).RespondFn(func(req *http.Request) (*http.Response, error) { | ||
return mocks.CreateEmptyHttpResponse(req, http.StatusNotFound) | ||
}) | ||
|
||
source, err := NewUrlTemplateSource(context.Background(), name, url, mockContext.HttpClient) | ||
require.Nil(t, source) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.