Example #1
0
File: env_test.go Project: ab22/env
// Test when parsing invalid types.
// env.Parse should return an *env.ErrInvalidInterface.
func TestInvalidInterfaces(t *testing.T) {
	values := []interface{}{
		"string type",
		1234,
		float32(4321.12),
		true,
		nil,
		SupportedTypes{},
	}

	for _, v := range values {
		// Parse by value
		if err := env.Parse(v); err == nil {
			t.Errorf("Accepting invalid type:", reflect.TypeOf(v))
		} else if err != env.ErrInvalidInterface {
			t.Fatal("Error should be of *env.ErrInvalidInterface but was:", reflect.TypeOf(v))
		}

		// Parse by reference
		if err := env.Parse(&v); err == nil {
			t.Errorf("env.Parse accepting invalid type by reference:", reflect.TypeOf(&v))
		} else if err != env.ErrInvalidInterface {
			t.Fatal("Error should be of *env.ErrInvalidInterface but was:", reflect.TypeOf(v))
		}
	}
}
Example #2
0
func NewConfig() *Config {
	var (
		adminAppFolder string
		err            error

		cfg = &Config{
			DbLogMode:    false,
			NoReplyEmail: "*****@*****.**",
		}
	)

	if err = env.Parse(cfg); err != nil {
		log.Fatalln(err)
	}

	if cfg.App.Env == "DEV" {
		adminAppFolder = "app"
	} else {
		adminAppFolder = "dist"
	}

	cfg.App.Frontend.Admin = path.Join("frontend/admin", adminAppFolder)

	return cfg
}
Example #3
0
File: env_test.go Project: ab22/env
// Test a struct with no env or envDefault tags set.
// It should not set any values to it and the values in the struct
// should be the default values that Go sets up.
func TestNoTagsSet(t *testing.T) {
	s := &NoTagValues{}

	if err := env.Parse(s); err != nil {
		t.Fatalf("Error parsing struct with no tags set: %v", err.Error())
	}

	if s.BoolType != false {
		t.Errorf("Test default values: bool value was not set properly. Expected: [%v] but was [%v]", false, s.BoolType)
	}

	if s.FloatType != 0 {
		t.Errorf("Test default values: float value was not set properly. Expected: [%v] but was [%v]", 0, s.FloatType)
	}

	if s.IntType != 0 {
		t.Errorf("Test default values: int value was not set properly. Expected: [%v] but was [%v]", 0, s.IntType)
	}

	if s.StringType != "" {
		t.Errorf("Test default values: string value was not set properly. Expected: [%v] but was [%v]", "", s.StringType)
	}

	if s.AnotherStruct.FloatType != 0 {
		t.Errorf("Test default values: float value was not set properly. Expected: [%v] but was [%v]", 0, s.AnotherStruct.FloatType)
	}

	if s.AnotherStruct.IntType != 0 {
		t.Errorf("Test default values: int value was not set properly. Expected: [%v] but was [%v]", 0, s.AnotherStruct.IntType)
	}
}
Example #4
0
File: env_test.go Project: ab22/env
// Test when parsing a struct that has an unaccesible/unexported field struct.
func TestRecursiveUnaccesibleStruct(t *testing.T) {
	var err error
	s := &RecursiveUnexportedStruct{}

	if err = env.Parse(s); err != nil {
		_, ok := err.(env.ErrFieldMustBeAssignable)

		if ok {
			return
		}
	}

	t.Errorf("Expected: [*env.FieldMustBeAssignable] but got: [%s] ", err)
}
Example #5
0
File: env_test.go Project: ab22/env
// Test a struct with a unsupported type
// It should return a *ErrUnsupportedFieldKind.
func TestUnSupportedField(t *testing.T) {
	var err error
	s := &UnSupportedField{}

	if err = env.Parse(s); err != nil {
		_, ok := err.(*env.ErrUnsupportedFieldKind)

		if ok {
			return
		}
	}

	t.Errorf("Expected: [*env.ErrUnsupportedFieldKind] but got: [%s] ", err)
}
Example #6
0
File: env_test.go Project: ab22/env
// Test a struct with no accesible fields.
// It should return a *ErrFieldMustBeAssignable.
func TestNonAssignableFields(t *testing.T) {
	var err error
	s := &NonaccesibleFields{}

	if err = env.Parse(s); err != nil {
		_, ok := err.(env.ErrFieldMustBeAssignable)

		if ok {
			return
		}
	}

	t.Errorf("Expected: [*env.FieldMustBeAssignable] but got: [%s] ", err)
}
Example #7
0
File: main.go Project: ab22/go-site
func main() {
	config := &AppConfig{}

	log.Println("Stating server...")

	env.Parse(config)
	log.Println("Server port:", config.Port)
	log.Println("App environment:", config.Env)

	templatesFilesHandler := http.FileServer(http.Dir("html/templates"))
	staticFilesHandler := noDirListing(http.FileServer(http.Dir("html/static/")))

	http.Handle("/", templatesFilesHandler)
	http.Handle("/static/", http.StripPrefix("/static/", staticFilesHandler))

	log.Println("Listening...")
	http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}
Example #8
0
File: env_test.go Project: ab22/env
// Test when parsing a struct that has an struct. Values should be set
// recursively into the embedded struct.
func TestRecursiveStructs(t *testing.T) {
	s := &RecursiveStruct{}

	if err := env.Parse(s); err != nil {
		t.Fatalf("Parsing struct by reference: %v", err.Error())
	}

	if s.StringType != defaultString {
		t.Errorf("Test recursive structs: string value was not set properly. Expected: [%v] but was [%v]", defaultString, s.StringType)
	}

	if s.AnotherStruct.IntType != defaultInt {
		t.Errorf("Test recursive structs: int value was not set properly. Expected: [%v] but was [%v]", defaultInt, s.AnotherStruct.IntType)
	}

	if s.AnotherStruct.FloatType != defaultFloat {
		t.Errorf("Test recursive structs: float value was not set properly. Expected: [%v] but was [%v]", defaultFloat, s.AnotherStruct.FloatType)
	}
}
Example #9
0
File: env_test.go Project: ab22/env
// Test a struct with no env value set.
// It should set the default values of the structure to the fields.
func TestEnvDefaultValues(t *testing.T) {
	s := &SupportedTypes{}

	if err := env.Parse(s); err != nil {
		t.Fatalf("Parsing struct by reference: %v", err.Error())
	}

	if s.BoolType != defaultBool {
		t.Errorf("Test envDefault values: bool value was not set properly. Expected: [%v] but was [%v]", defaultBool, s.BoolType)
	}

	if s.FloatType != defaultFloat {
		t.Errorf("Test envDefault values: float value was not set properly. Expected: [%v] but was [%v]", defaultFloat, s.FloatType)
	}

	if s.IntType != defaultInt {
		t.Errorf("Test envDefault values: int value was not set properly. Expected: [%v] but was [%v]", defaultInt, s.IntType)
	}

	if s.StringType != defaultString {
		t.Errorf("Test envDefault values: string value was not set properly. Expected: [%v] but was [%v]", defaultString, s.StringType)
	}
}
Example #10
0
File: env_test.go Project: ab22/env
// Test when parsing env variables into a struct.
// It should set the env values into the structure.
func TestEnvironmentValues(t *testing.T) {
	stringVar := "string value"
	intVar := 6789
	floatVar := float32(1234.56)
	boolVar := true
	s := &SupportedTypes{}

	os.Setenv("STRING_VAR", stringVar)
	os.Setenv("INT_VAR", fmt.Sprintf("%d", intVar))
	os.Setenv("FLOAT_VAR", fmt.Sprintf("%f", floatVar))
	os.Setenv("BOOL_VAR", fmt.Sprintf("%t", boolVar))
	defer func() {
		syscall.Clearenv()
	}()

	if err := env.Parse(s); err != nil {
		t.Fatal(err.Error())
	}

	if s.BoolType != boolVar {
		t.Errorf("Test env values: bool value was not set properly. Expected: [%v] but was [%v]", boolVar, s.BoolType)
	}

	if s.FloatType != floatVar {
		t.Errorf("Test env values: float value was not set properly. Expected: [%v] but was [%v]", floatVar, s.FloatType)
	}

	if s.IntType != intVar {
		t.Errorf("Test env values: int value was not set properly. Expected: [%v] but was [%v]", intVar, s.IntType)
	}

	if s.StringType != stringVar {
		t.Errorf("Test env values: string value was not set properly. Expected: [%v] but was [%v]", stringVar, s.StringType)
	}

}