Skip to content

Commit

Permalink
Relative pathc reference test
Browse files Browse the repository at this point in the history
  • Loading branch information
godrei committed Jul 11, 2024
1 parent 35a65ee commit 98a52d1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 38 deletions.
24 changes: 12 additions & 12 deletions configmerge/configmerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ type Merger struct {
fileReader FileReader
logger logV2.Logger

repoInfo *RepoInfo
mainConfigDir string
repoInfo *RepoInfo

filesCount int
}
Expand All @@ -69,7 +68,6 @@ func NewMerger(repoInfoProvider RepoInfoProvider, fileReader FileReader, logger

func (m *Merger) MergeConfig(mainConfigPth string) (string, *models.ConfigFileTreeModel, error) {
repoDir := filepath.Dir(mainConfigPth)
m.mainConfigDir = repoDir

repoInfo, err := m.repoInfoProvider.GetRepoInfo(repoDir)
if err != nil {
Expand All @@ -94,7 +92,8 @@ func (m *Merger) MergeConfig(mainConfigPth string) (string, *models.ConfigFileTr
return "", nil, err
}

configTree, err := m.buildConfigTree(mainConfigBytes, mainConfigRef, 1, nil)
mainConfigDir := filepath.Dir(mainConfigPth)
configTree, err := m.buildConfigTree(mainConfigBytes, mainConfigRef, mainConfigDir, 1, nil)
if err != nil {
return "", nil, err
}
Expand All @@ -107,7 +106,7 @@ func (m *Merger) MergeConfig(mainConfigPth string) (string, *models.ConfigFileTr
return mergedConfigContent, configTree, nil
}

func (m *Merger) buildConfigTree(configContent []byte, reference ConfigReference, depth int, keys []string) (*models.ConfigFileTreeModel, error) {
func (m *Merger) buildConfigTree(configContent []byte, reference ConfigReference, dir string, depth int, keys []string) (*models.ConfigFileTreeModel, error) {
key := reference.Key()
keys = append(keys, key)

Expand Down Expand Up @@ -157,12 +156,13 @@ func (m *Merger) buildConfigTree(configContent []byte, reference ConfigReference

var includedConfigTrees []models.ConfigFileTreeModel
for _, include := range config.Include {
moduleBytes, err := m.readConfigModule(include, m.repoInfo)
moduleBytes, err := m.readConfigModule(include, dir, m.repoInfo)
if err != nil {
return nil, err
}

moduleConfigTree, err := m.buildConfigTree(moduleBytes, include, depth+1, keys)
moduleDir := filepath.Dir(include.Path)
moduleConfigTree, err := m.buildConfigTree(moduleBytes, include, moduleDir, depth+1, keys)
if err != nil {
return nil, err
}
Expand All @@ -178,9 +178,9 @@ func (m *Merger) buildConfigTree(configContent []byte, reference ConfigReference
}, nil
}

func (m *Merger) readConfigModule(reference ConfigReference, repoInfo *RepoInfo) ([]byte, error) {
func (m *Merger) readConfigModule(reference ConfigReference, dir string, repoInfo *RepoInfo) ([]byte, error) {
if isLocalReference(reference) {
return m.readLocalConfigModule(reference)
return m.readLocalConfigModule(reference, dir)
}

sameRepo := false
Expand All @@ -192,7 +192,7 @@ func (m *Merger) readConfigModule(reference ConfigReference, repoInfo *RepoInfo)
}

if sameRepo {
return m.readLocalConfigModule(reference)
return m.readLocalConfigModule(reference, dir)
}

return m.readRemoteConfigModule(reference)
Expand Down Expand Up @@ -230,10 +230,10 @@ func isLocalReference(reference ConfigReference) bool {
return reference.Repository == ""
}

func (m *Merger) readLocalConfigModule(reference ConfigReference) ([]byte, error) {
func (m *Merger) readLocalConfigModule(reference ConfigReference, dir string) ([]byte, error) {
pth := reference.Path
if !filepath.IsAbs(pth) {
pth = filepath.Join(m.mainConfigDir, pth)
pth = filepath.Join(dir, pth)
}
return m.fileReader.ReadFileFromFileSystem(pth)
}
Expand Down
89 changes: 63 additions & 26 deletions configmerge/configmerge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ include:
%s`, strings.Repeat("- path: path_1.yml\n", 10))),
"path_1.yml": []byte(`include:
- path: path_2.yml`),
"path_2.yml": []byte(``),
},
},
mainConfigPth: "bitrise.yml",
Expand Down Expand Up @@ -211,6 +212,44 @@ include:
image: golang:1.22
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
format_version: "15"
`,
},
{
name: "Follows references' relative paths",
repoInfoProvider: mockRepoInfoProvider{
repoInfo: &RepoInfo{
DefaultRemoteURL: "https://github.com/bitrise-io/example.git",
Branch: "main",
Commit: "016883ca9498f75d03cd45c0fa400ad9f8141edf",
},
err: nil,
},
fileReader: mockFileReader{
fileSystemFiles: map[string][]byte{
"bitrise.yml": []byte(`format_version: "15"
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
include:
- path: configs/ci/module_1.yml`),
"configs/ci/module_1.yml": []byte(`include:
- path: module_2.yml`),
"configs/ci/module_2.yml": []byte(`workflows:
print_hello:
steps:
- script:
inputs:
- content: echo "Hello World!"`),
},
},
mainConfigPth: "bitrise.yml",
wantConfig: `default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
format_version: "15"
workflows:
print_hello:
steps:
- script:
inputs:
- content: echo "Hello World!"
`,
},
{
Expand Down Expand Up @@ -284,46 +323,44 @@ func (m mockRepoInfoProvider) GetRepoInfo(repoPth string) (*RepoInfo, error) {

type mockFileReader struct {
fileSystemFiles map[string][]byte
fileSystemErr error
repoFilesOnCommit map[string]map[string]map[string][]byte
repoFilesOnTag map[string]map[string]map[string][]byte
repoFilesOnBranch map[string]map[string]map[string][]byte
repoErr error
}

func (m mockFileReader) ReadFileFromFileSystem(name string) ([]byte, error) {
return m.fileSystemFiles[name], m.fileSystemErr
c, ok := m.fileSystemFiles[name]
if !ok {
return nil, fmt.Errorf("file not found: %s", name)
}
return c, nil
}

func (m mockFileReader) ReadFileFromGitRepository(repository string, branch string, commit string, tag string, path string) ([]byte, error) {
var repoFiles map[string]map[string]map[string][]byte
var checkout string
if commit != "" {
filesInRepo, ok := m.repoFilesOnCommit[repository]
if !ok {
return nil, m.repoErr
}
filesOnCommit, ok := filesInRepo[commit]
if !ok {
return nil, m.repoErr
}
return filesOnCommit[path], m.repoErr
repoFiles = m.repoFilesOnCommit
checkout = commit
} else if tag != "" {
filesInRepo, ok := m.repoFilesOnTag[repository]
if !ok {
return nil, m.repoErr
}
filesOnTag, ok := filesInRepo[tag]
if !ok {
return nil, m.repoErr
}
return filesOnTag[path], m.repoErr
repoFiles = m.repoFilesOnTag
checkout = tag
} else {
repoFiles = m.repoFilesOnBranch
checkout = branch
}

filesInRepo, ok := repoFiles[repository]
if !ok {
return nil, fmt.Errorf("repo not found: %s", repository)
}
filesInRepo, ok := m.repoFilesOnBranch[repository]
filesOnCommit, ok := filesInRepo[checkout]
if !ok {
return nil, m.repoErr
return nil, fmt.Errorf("checkout param not found: %s", checkout)
}
filesOnBranch, ok := filesInRepo[branch]
c, ok := filesOnCommit[path]
if !ok {
return nil, m.repoErr
return nil, fmt.Errorf("file not found: %s", path)
}
return filesOnBranch[path], m.repoErr
return c, nil
}

0 comments on commit 98a52d1

Please sign in to comment.