github.com/astaxie/beego@v1.12.3/config/env/env_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  // Copyright 2017 Faissal Elamraoui. All Rights Reserved.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  package env
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  )
    21  
    22  func TestEnvGet(t *testing.T) {
    23  	gopath := Get("GOPATH", "")
    24  	if gopath != os.Getenv("GOPATH") {
    25  		t.Error("expected GOPATH not empty.")
    26  	}
    27  
    28  	noExistVar := Get("NOEXISTVAR", "foo")
    29  	if noExistVar != "foo" {
    30  		t.Errorf("expected NOEXISTVAR to equal foo, got %s.", noExistVar)
    31  	}
    32  }
    33  
    34  func TestEnvMustGet(t *testing.T) {
    35  	gopath, err := MustGet("GOPATH")
    36  	if err != nil {
    37  		t.Error(err)
    38  	}
    39  
    40  	if gopath != os.Getenv("GOPATH") {
    41  		t.Errorf("expected GOPATH to be the same, got %s.", gopath)
    42  	}
    43  
    44  	_, err = MustGet("NOEXISTVAR")
    45  	if err == nil {
    46  		t.Error("expected error to be non-nil")
    47  	}
    48  }
    49  
    50  func TestEnvSet(t *testing.T) {
    51  	Set("MYVAR", "foo")
    52  	myVar := Get("MYVAR", "bar")
    53  	if myVar != "foo" {
    54  		t.Errorf("expected MYVAR to equal foo, got %s.", myVar)
    55  	}
    56  }
    57  
    58  func TestEnvMustSet(t *testing.T) {
    59  	err := MustSet("FOO", "bar")
    60  	if err != nil {
    61  		t.Error(err)
    62  	}
    63  
    64  	fooVar := os.Getenv("FOO")
    65  	if fooVar != "bar" {
    66  		t.Errorf("expected FOO variable to equal bar, got %s.", fooVar)
    67  	}
    68  }
    69  
    70  func TestEnvGetAll(t *testing.T) {
    71  	envMap := GetAll()
    72  	if len(envMap) == 0 {
    73  		t.Error("expected environment not empty.")
    74  	}
    75  }