Skip to content

Commit

Permalink
Code cleanup and modular config integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
godrei committed Jul 11, 2024
1 parent 9d04fc1 commit e9d0f84
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
14 changes: 14 additions & 0 deletions _tests/integration/modular_config_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
format_version: "15"
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git

include:
- path: modular_config_module.yml

workflows:
print_hello_bitrise:
envs:
- NAME: Bitrise
steps:
- script:
inputs:
- content: echo "Hello $NAME!"
8 changes: 8 additions & 0 deletions _tests/integration/modular_config_module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
workflows:
print_hello_world:
envs:
- NAME: World
steps:
- script:
inputs:
- content: echo "Hello $NAME!"
32 changes: 32 additions & 0 deletions _tests/integration/modular_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package integration

import (
"testing"

"github.com/bitrise-io/go-utils/command"
"github.com/stretchr/testify/require"
)

func Test_ModularConfig_Run(t *testing.T) {
configPth := "modular_config_main.yml"

cmd := command.New(binPath(), "validate", "--config", configPth)
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
require.NoError(t, err, out)
require.Equal(t, "Config is valid: \u001B[32;1mtrue\u001B[0m", out)

cmd = command.New(binPath(), "workflows", "--id-only", "--config", configPth)
out, err = cmd.RunAndReturnTrimmedCombinedOutput()
require.NoError(t, err, out)
require.Equal(t, "print_hello_bitrise print_hello_world", out)

cmd = command.New(binPath(), "run", "print_hello_bitrise", "--config", configPth)
out, err = cmd.RunAndReturnTrimmedCombinedOutput()
require.NoError(t, err, out)
require.Contains(t, out, "Hello Bitrise!")

cmd = command.New(binPath(), "run", "print_hello_world", "--config", configPth)
out, err = cmd.RunAndReturnTrimmedCombinedOutput()
require.NoError(t, err, out)
require.Contains(t, out, "Hello World!")
}
48 changes: 30 additions & 18 deletions configmerge/configmerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"strings"

"github.com/bitrise-io/bitrise/log"

"github.com/bitrise-io/bitrise/models"
"github.com/bitrise-io/go-utils/sliceutil"
logV2 "github.com/bitrise-io/go-utils/v2/log"
Expand Down Expand Up @@ -53,7 +55,7 @@ type Merger struct {
fileReader FileReader
logger logV2.Logger

repoInfo RepoInfo
repoInfo *RepoInfo

filesCount int
}
Expand All @@ -70,16 +72,20 @@ func (m *Merger) MergeConfig(mainConfigPth string) (string, *models.ConfigFileTr
repoDir := filepath.Dir(mainConfigPth)
repoInfo, err := m.repoInfoProvider.GetRepoInfo(repoDir)
if err != nil {
return "", nil, err
log.Debug("Failed to get repository info: %s", err)
} else {
m.repoInfo = repoInfo
}
m.repoInfo = *repoInfo

mainConfigRef := ConfigReference{
Repository: repoInfo.DefaultRemoteURL,
Commit: repoInfo.Commit,
Tag: repoInfo.Tag,
Branch: repoInfo.Branch,
Path: mainConfigPth,
Path: mainConfigPth,
}

if repoInfo != nil {
mainConfigRef.Repository = repoInfo.DefaultRemoteURL
mainConfigRef.Commit = repoInfo.Commit
mainConfigRef.Tag = repoInfo.Tag
mainConfigRef.Branch = repoInfo.Branch
}

mainConfigBytes, err := m.fileReader.ReadFileFromFileSystem(mainConfigPth)
Expand Down Expand Up @@ -171,27 +177,33 @@ func (m *Merger) buildConfigTree(configContent []byte, reference ConfigReference
}, nil
}

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

if sameRepo, err := isSameRepoReference(reference, info); err != nil {
m.logger.Warnf("Failed to check if the reference is from the same repository: %s", err)
} else if sameRepo {
sameRepo := false
if repoInfo != nil {
var err error
if sameRepo, err = isSameRepoReference(reference, *repoInfo); err != nil {
m.logger.Warnf("Failed to check if the reference is from the same repository: %s", err)
}
}

if sameRepo {
return m.readLocalConfigModule(reference)
}

return m.readRemoteConfigModule(reference)
}

func isSameRepoReference(reference ConfigReference, info RepoInfo) (bool, error) {
func isSameRepoReference(reference ConfigReference, repoInfo RepoInfo) (bool, error) {
refGitUrl, err := parseGitRepoURL(reference.Repository)
if err != nil {
return false, err
}

repoGitURL, err := parseGitRepoURL(info.DefaultRemoteURL)
repoGitURL, err := parseGitRepoURL(repoInfo.DefaultRemoteURL)
if err != nil {
return false, err
}
Expand All @@ -202,12 +214,12 @@ func isSameRepoReference(reference ConfigReference, info RepoInfo) (bool, error)

switch {
case reference.Commit != "":
return reference.Commit == info.Commit ||
reference.Commit == info.Commit[:7], nil
return reference.Commit == repoInfo.Commit ||
reference.Commit == repoInfo.Commit[:7], nil
case reference.Tag != "":
return reference.Tag == info.Tag, nil
return reference.Tag == repoInfo.Tag, nil
case reference.Branch != "":
return reference.Branch == info.Branch, nil
return reference.Branch == repoInfo.Branch, nil
}

return true, nil
Expand Down
4 changes: 2 additions & 2 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const (

const (
selfUpdateInterval = 24 * time.Hour
pluginUpdateInterval = 24 * time.Hour
PluginUpdateInterval = 24 * time.Hour
)

// IsDebugUseSystemTools ...
Expand Down Expand Up @@ -164,7 +164,7 @@ func CheckIsPluginUpdateCheckRequired(plugin string) bool {
}

duration := time.Now().Sub(config.LastPluginUpdateChecks[plugin])
if duration >= pluginUpdateInterval {
if duration >= PluginUpdateInterval {
return true
}

Expand Down

0 comments on commit e9d0f84

Please sign in to comment.