Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#265] The Config module supports get system environment variables #329

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

func NewApplication(envPath string) *Application {
if !file.Exists(envPath) {
color.Redln("Please create " + envPath + " and initialize it first.")
color.Warnln("Example command: \ncp .env.example .env && go run . artisan key:generate")

Check warning on line 24 in config/application.go

View check run for this annotation

Codecov / codecov/patch

config/application.go#L23-L24

Added lines #L23 - L24 were not covered by tests
os.Exit(0)
}

Expand All @@ -29,26 +29,24 @@
app.vip = viper.New()
app.vip.SetConfigType("env")
app.vip.SetConfigFile(envPath)
app.vip.AutomaticEnv()

if err := app.vip.ReadInConfig(); err != nil {
color.Redln("Invalid Config error: " + err.Error())
os.Exit(0)
}

app.vip.SetEnvPrefix("goravel")
app.vip.AutomaticEnv()

appKey := app.Env("APP_KEY")
if support.Env != support.EnvArtisan {
if appKey == nil {
color.Redln("Please initialize APP_KEY first.")
color.Warnln("Example command: \ngo run . artisan key:generate")

Check warning on line 43 in config/application.go

View check run for this annotation

Codecov / codecov/patch

config/application.go#L43

Added line #L43 was not covered by tests
os.Exit(0)
}

if len(appKey.(string)) != 32 {
color.Redln("Invalid APP_KEY, please reset it.")
color.Redln("Invalid APP_KEY, the length must be 32, please reset it.")
color.Warnln("Example command: \ngo run . artisan key:generate")

Check warning on line 49 in config/application.go

View check run for this annotation

Codecov / codecov/patch

config/application.go#L48-L49

Added lines #L48 - L49 were not covered by tests
os.Exit(0)
}
}
Expand Down
82 changes: 54 additions & 28 deletions config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ import (

type ApplicationTestSuite struct {
suite.Suite
config *Application
config2 *Application
config *Application
customConfig *Application
}

func TestApplicationTestSuite(t *testing.T) {
assert.Nil(t, file.Create(".env", "APP_KEY=12345678901234567890123456789012"))
assert.Nil(t, file.Create(".env", `
APP_KEY=12345678901234567890123456789012
APP_DEBUG=true
DB_PORT=3306
`))
temp, err := os.CreateTemp("", "goravel.env")
assert.Nil(t, err)
defer os.Remove(temp.Name())
_, err = temp.Write([]byte("APP_KEY=12345678901234567890123456789012"))
_, err = temp.Write([]byte(`
APP_KEY=12345678901234567890123456789012
APP_DEBUG=true
DB_PORT=3306
`))
assert.Nil(t, err)
assert.Nil(t, temp.Close())

suite.Run(t, &ApplicationTestSuite{
config: NewApplication(".env"),
config2: NewApplication(temp.Name()),
config: NewApplication(".env"),
customConfig: NewApplication(temp.Name()),
})

assert.Nil(t, file.Remove(".env"))
Expand All @@ -37,43 +45,61 @@ func (s *ApplicationTestSuite) SetupTest() {

}

func (s *ApplicationTestSuite) TestOsVariables() {
s.Nil(os.Setenv("APP_KEY", "12345678901234567890123456789013"))
s.Nil(os.Setenv("OS_APP_NAME", "goravel"))
s.Nil(os.Setenv("OS_APP_PORT", "3306"))
s.Nil(os.Setenv("OS_APP_DEBUG", "true"))

s.Equal("12345678901234567890123456789013", s.config.GetString("APP_KEY"))
s.Equal("12345678901234567890123456789013", s.customConfig.GetString("APP_KEY"))
s.Equal("goravel", s.config.GetString("OS_APP_NAME"))
s.Equal("goravel", s.customConfig.GetString("OS_APP_NAME"))
s.Equal(3306, s.config.GetInt("OS_APP_PORT"))
s.Equal(3306, s.customConfig.GetInt("OS_APP_PORT"))
s.True(s.config.GetBool("OS_APP_DEBUG"))
s.True(s.customConfig.GetBool("OS_APP_DEBUG"))
}

func (s *ApplicationTestSuite) TestEnv() {
s.Equal("12345678901234567890123456789012", s.config.Env("APP_KEY").(string))
s.Equal("goravel", s.config.Env("APP_NAME", "goravel").(string))
s.Equal("127.0.0.1", s.config.Env("DB_HOST", "127.0.0.1").(string))
s.Equal("goravel", s.config2.Env("APP_NAME", "goravel").(string))
s.Equal("127.0.0.1", s.config2.Env("DB_HOST", "127.0.0.1").(string))
s.Equal("12345678901234567890123456789012", s.customConfig.Env("APP_KEY").(string))
s.Equal("goravel", s.customConfig.Env("APP_NAME", "goravel").(string))
}

func (s *ApplicationTestSuite) TestAdd() {
s.config.Add("app", map[string]any{
"env": "local",
})
s.config2.Add("app", map[string]any{
s.customConfig.Add("app", map[string]any{
"env": "local",
})

s.Equal("local", s.config.GetString("app.env"))
s.Equal("local", s.config2.GetString("app.env"))
s.Equal("local", s.customConfig.GetString("app.env"))

s.config.Add("path.with.dot.case1", "value1")
s.config2.Add("path.with.dot.case1", "value1")
s.customConfig.Add("path.with.dot.case1", "value1")
s.Equal("value1", s.config.GetString("path.with.dot.case1"))
s.Equal("value1", s.config2.GetString("path.with.dot.case1"))
s.Equal("value1", s.customConfig.GetString("path.with.dot.case1"))

s.config.Add("path.with.dot.case2", "value2")
s.config2.Add("path.with.dot.case2", "value2")
s.customConfig.Add("path.with.dot.case2", "value2")
s.Equal("value2", s.config.GetString("path.with.dot.case2"))
s.Equal("value2", s.config2.GetString("path.with.dot.case2"))
s.Equal("value2", s.customConfig.GetString("path.with.dot.case2"))

s.config.Add("path.with.dot", map[string]any{"case3": "value3"})
s.config2.Add("path.with.dot", map[string]any{"case3": "value3"})
s.customConfig.Add("path.with.dot", map[string]any{"case3": "value3"})
s.Equal("value3", s.config.GetString("path.with.dot.case3"))
s.Equal("value3", s.config2.GetString("path.with.dot.case3"))
s.Equal("value3", s.customConfig.GetString("path.with.dot.case3"))
}

func (s *ApplicationTestSuite) TestGet() {
s.Equal("12345678901234567890123456789012", s.config.Get("APP_KEY").(string))
s.Equal("goravel", s.config.Get("APP_NAME", "goravel").(string))
s.Equal("goravel", s.config2.Get("APP_NAME", "goravel").(string))
s.Equal("12345678901234567890123456789012", s.customConfig.Get("APP_KEY").(string))
s.Equal("goravel", s.customConfig.Get("APP_NAME", "goravel").(string))
}

func (s *ApplicationTestSuite) TestGetString() {
Expand All @@ -85,29 +111,29 @@ func (s *ApplicationTestSuite) TestGetString() {
},
},
})
s.config2.Add("database", map[string]any{
"default": s.config2.Env("DB_CONNECTION", "mysql"),
s.customConfig.Add("database", map[string]any{
"default": s.customConfig.Env("DB_CONNECTION", "mysql"),
"connections": map[string]any{
"mysql": map[string]any{
"host": s.config2.Env("DB_HOST", "127.0.0.1"),
"host": s.customConfig.Env("DB_HOST", "127.0.0.1"),
},
},
})

s.Equal("goravel", s.config.GetString("APP_NAME", "goravel"))
s.Equal("127.0.0.1", s.config.GetString("database.connections.mysql.host"))
s.Equal("mysql", s.config.GetString("database.default"))
s.Equal("goravel", s.config2.GetString("APP_NAME", "goravel"))
s.Equal("127.0.0.1", s.config2.GetString("database.connections.mysql.host"))
s.Equal("mysql", s.config2.GetString("database.default"))
s.Equal("goravel", s.customConfig.GetString("APP_NAME", "goravel"))
s.Equal("127.0.0.1", s.customConfig.GetString("database.connections.mysql.host"))
s.Equal("mysql", s.customConfig.GetString("database.default"))
}

func (s *ApplicationTestSuite) TestGetInt() {
s.Equal(s.config.GetInt("DB_PORT", 3306), 3306)
s.Equal(s.config2.GetInt("DB_PORT", 3306), 3306)
s.Equal(3306, s.config.GetInt("DB_PORT"))
s.Equal(3306, s.customConfig.GetInt("DB_PORT"))
}

func (s *ApplicationTestSuite) TestGetBool() {
s.Equal(true, s.config.GetBool("APP_DEBUG", true))
s.Equal(true, s.config2.GetBool("APP_DEBUG", true))
s.Equal(true, s.config.GetBool("APP_DEBUG"))
s.Equal(true, s.customConfig.GetBool("APP_DEBUG"))
}
Loading