Skip to content

Commit

Permalink
Merge pull request #162 from zeabur/pan93412/ZEA-2035
Browse files Browse the repository at this point in the history
refactor(planner): Use JSON as the format of zbpack configuration
  • Loading branch information
yuaanlin authored Oct 20, 2023
2 parents ded8ad3 + 4d6349c commit 55b1ffb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/plan/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ViperProjectConfiguration struct {
// NewProjectConfiguration creates a new ViperProjectConfiguration.
func NewProjectConfiguration() ProjectConfiguration {
viperInstance := viper.New()
viperInstance.SetConfigType("toml")
viperInstance.SetConfigType("json")

return &ViperProjectConfiguration{
Viper: viperInstance,
Expand Down Expand Up @@ -80,7 +80,7 @@ func NewProjectConfigurationFromFs(fs afero.Fs) ProjectConfiguration {

// ReadFromFs reads the configuration from the given file system.
func (vpc *ViperProjectConfiguration) ReadFromFs(fs afero.Fs) error {
file, err := fs.Open("zbpack.toml")
file, err := fs.Open("zbpack.json")
if err != nil {
return fmt.Errorf("open config file: %w", err)
}
Expand Down
51 changes: 32 additions & 19 deletions pkg/plan/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ func TestProjectConfiguration_Empty(t *testing.T) {
assert.False(t, config.IsSet("laravel.owo"))
}

func TestProjectConfiguration_ZbpackTomlExisted(t *testing.T) {
func TestProjectConfiguration_ZbpackJsonExisted(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[laravel]
test = "owo"`), 0644)
_ = afero.WriteFile(fs, "zbpack.json", []byte(`{"laravel":{"test": "owo" }}`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)

Expand All @@ -33,7 +32,7 @@ test = "owo"`), 0644)
assert.False(t, config.IsSet("laravel.owo"))
}

func TestProjectConfiguration_ZbpackTomlNotExisted(t *testing.T) {
func TestProjectConfiguration_ZbpackJsonNotExisted(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
Expand All @@ -50,8 +49,8 @@ func TestGetProjectConfigValue_Global(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[project]
build_command = "build"`), 0644)

_ = afero.WriteFile(fs, "zbpack.json", []byte(`{"project": { "build_command": "build" } }`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)
assert.Equal(t, optional.Some("build"), plan.GetProjectConfigValue(config, "", "build_command"))
Expand All @@ -61,8 +60,13 @@ func TestGetProjectConfigValue_Submodule(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[project.sm]
build_command = "build.sm"`), 0644)
_ = afero.WriteFile(fs, "zbpack.json", []byte(`{
"project": {
"sm": {
"build_command": "build.sm"
}
}
}`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)
assert.Equal(t, optional.Some("build.sm"), plan.GetProjectConfigValue(config, "sm", "build_command"))
Expand All @@ -72,10 +76,14 @@ func TestGetProjectConfigValue_SubmoduleOverride(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[project]
build_command = "build"
[project.sm]
build_command = "build.sm"`), 0644)
_ = afero.WriteFile(fs, "zbpack.json", []byte(`{
"project": {
"build_command": "build",
"sm": {
"build_command": "build.sm"
}
}
}`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)
assert.Equal(t, optional.Some("build.sm"), plan.GetProjectConfigValue(config, "sm", "build_command"))
Expand All @@ -88,9 +96,12 @@ func TestGetProjectConfigValue_SubmoduleFallback(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[project]
build_command = "build"
[project.sm]`), 0644)
_ = afero.WriteFile(fs, "zbpack.json", []byte(`{
"project": {
"build_command": "build",
"sm": {}
}
}`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)
assert.Equal(t, optional.Some("build"), plan.GetProjectConfigValue(config, "sm", "build_command"))
Expand All @@ -100,8 +111,11 @@ build_command = "build"
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[project]
build_command = "build"`), 0644)
_ = afero.WriteFile(fs, "zbpack.json", []byte(`{
"project": {
"build_command": "build"
}
}`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)
assert.Equal(t, optional.Some("build"), plan.GetProjectConfigValue(config, "sm", "build_command"))
Expand All @@ -112,8 +126,7 @@ func TestGetProjectConfigValue_None(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
_ = afero.WriteFile(fs, "zbpack.toml", []byte(`[project]
`), 0644)
_ = afero.WriteFile(fs, "zbpack.json", []byte(`{"project":{}}`), 0644)

config := plan.NewProjectConfigurationFromFs(fs)
assert.Equal(t, optional.None[string](), plan.GetProjectConfigValue(config, "", "build_command"))
Expand Down

0 comments on commit 55b1ffb

Please sign in to comment.