github.com/qapquiz/xk6-dotenv@v0.1.2-0.20220614041208-d05c37e5dd7e/dotenv_internal_test.go (about)

     1  // MIT License
     2  //
     3  // Copyright (c) 2021 Iván Szkiba
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    21  // SOFTWARE.
    22  
    23  package dotenv
    24  
    25  import (
    26  	"os"
    27  	"testing"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  const k6env = "K6_ENV"
    33  
    34  func TestMain(m *testing.M) {
    35  	os.Chdir("test/testdata") // nolint:errcheck
    36  	os.Exit(m.Run())
    37  }
    38  
    39  func unset(t *testing.T) {
    40  	t.Helper()
    41  
    42  	assert.Nil(t, os.Unsetenv("global"))
    43  	assert.Nil(t, os.Unsetenv("common"))
    44  	assert.Nil(t, os.Unsetenv("local"))
    45  	assert.Nil(t, os.Unsetenv("localonly"))
    46  }
    47  
    48  func Test_default(t *testing.T) { // nolint:paralleltest
    49  	assert.Nil(t, os.Setenv(k6env, ""))
    50  	unset(t)
    51  	load()
    52  
    53  	assert.Equal(t, ".env", os.Getenv("global"))
    54  	assert.Equal(t, ".env.development", os.Getenv("common"))
    55  	assert.Equal(t, ".env.development.local", os.Getenv("local"))
    56  	assert.Equal(t, ".env.local", os.Getenv("localonly"))
    57  }
    58  
    59  func Test_disabled(t *testing.T) { // nolint:paralleltest
    60  	assert.Nil(t, os.Setenv(k6env, "false"))
    61  	unset(t)
    62  	load()
    63  
    64  	assert.Empty(t, os.Getenv("global"))
    65  	assert.Empty(t, os.Getenv("common"))
    66  	assert.Empty(t, os.Getenv("local"))
    67  	assert.Empty(t, os.Getenv("localonly"))
    68  }
    69  
    70  func Test_test(t *testing.T) { // nolint:paralleltest
    71  	os.Setenv(k6env, "test")
    72  	unset(t)
    73  	load()
    74  
    75  	assert.Equal(t, ".env", os.Getenv("global"))
    76  	assert.Equal(t, ".env.test", os.Getenv("common"))
    77  	assert.Equal(t, ".env.test.local", os.Getenv("local"))
    78  	assert.Empty(t, os.Getenv("localonly"))
    79  }
    80  
    81  func Test_production(t *testing.T) { // nolint:paralleltest
    82  	assert.Nil(t, os.Setenv(k6env, "production"))
    83  	unset(t)
    84  	load()
    85  
    86  	assert.Equal(t, ".env", os.Getenv("global"))
    87  	assert.Equal(t, ".env.production", os.Getenv("common"))
    88  	assert.Equal(t, ".env.production.local", os.Getenv("local"))
    89  	assert.Equal(t, ".env.local", os.Getenv("localonly"))
    90  }
    91  
    92  func Test_development(t *testing.T) { // nolint:paralleltest
    93  	assert.Nil(t, os.Setenv(k6env, "development"))
    94  	unset(t)
    95  	load()
    96  
    97  	assert.Equal(t, ".env", os.Getenv("global"))
    98  	assert.Equal(t, ".env.development", os.Getenv("common"))
    99  	assert.Equal(t, ".env.development.local", os.Getenv("local"))
   100  	assert.Equal(t, ".env.local", os.Getenv("localonly"))
   101  }