github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/internal/config/application_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  	"testing"
     8  
     9  	"github.com/adrg/xdg"
    10  	"github.com/mitchellh/go-homedir"
    11  	"github.com/spf13/viper"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // TODO: set negative case when config.yaml is no longer a valid option
    17  func TestApplicationConfig(t *testing.T) {
    18  	// disable homedir package cache for testing
    19  	originalCacheOpt := homedir.DisableCache
    20  	homedir.DisableCache = true
    21  	t.Cleanup(func() {
    22  		homedir.DisableCache = originalCacheOpt
    23  	})
    24  
    25  	// ensure we have no side effects for xdg package for future tests
    26  	originalXDG := os.Getenv("XDG_CONFIG_HOME")
    27  	t.Cleanup(func() {
    28  		// note: we're not using t.Setenv since the effect we're trying to eliminate is within the xdg package
    29  		require.NoError(t, os.Setenv("XDG_CONFIG_HOME", originalXDG))
    30  		xdg.Reload()
    31  	})
    32  
    33  	// config is picked up at desired configuration paths
    34  	// VALID: .buildx.yaml, .buildx/config.yaml, ~/.buildx.yaml, <XDG_CONFIG_HOME>/buildx/config.yaml
    35  	// DEPRECATED: config.yaml is currently supported by
    36  	tests := []struct {
    37  		name       string
    38  		setup      func(t *testing.T) string
    39  		assertions func(t *testing.T, app *Application)
    40  		cleanup    func()
    41  	}{
    42  		{
    43  			name: "explicit config",
    44  			setup: func(t *testing.T) string {
    45  				return "./test-fixtures/.buildx.yaml"
    46  			}, // no-op for explicit config
    47  			assertions: func(t *testing.T, app *Application) {
    48  				assert.Equal(t, "test-explicit-config", app.File)
    49  			},
    50  		},
    51  		{
    52  			name: "current working directory named config",
    53  			setup: func(t *testing.T) string {
    54  				err := os.Chdir("./test-fixtures/config-wd-file") // change application cwd to test-fixtures
    55  				require.NoError(t, err)
    56  				return ""
    57  			},
    58  			assertions: func(t *testing.T, app *Application) {
    59  				assert.Equal(t, "test-wd-named-config", app.File)
    60  			},
    61  		},
    62  		{
    63  			name: "current working directory buildx dir config",
    64  			setup: func(t *testing.T) string {
    65  				err := os.Chdir("./test-fixtures/config-dir-test") // change application cwd to test-fixtures
    66  				require.NoError(t, err)
    67  				return ""
    68  			},
    69  			assertions: func(t *testing.T, app *Application) {
    70  				assert.Equal(t, "test-dir-config", app.File)
    71  			},
    72  		},
    73  		{
    74  			name: "home directory file config",
    75  			setup: func(t *testing.T) string {
    76  				// Because Setenv affects the whole process, it cannot be used in parallel tests or
    77  				// tests with parallel ancestors: see separate XDG test for consequence of this
    78  				t.Setenv("HOME", "./test-fixtures/config-home-test/config-file")
    79  				return ""
    80  			},
    81  			assertions: func(t *testing.T, app *Application) {
    82  				assert.Equal(t, "test-home-config", app.File)
    83  			},
    84  		},
    85  		{
    86  			name: "XDG file config",
    87  			setup: func(t *testing.T) string {
    88  				wd, err := os.Getwd()
    89  				require.NoError(t, err)
    90  				configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata
    91  				// note: this explicitly has multiple XDG paths, make certain we use the first VALID one (not the first one)
    92  				t.Setenv("XDG_CONFIG_DIRS", fmt.Sprintf("/another/foo/bar:%s", configDir))
    93  				xdg.Reload()
    94  				return ""
    95  			},
    96  			assertions: func(t *testing.T, app *Application) {
    97  				assert.Equal(t, "test-home-XDG-config", app.File)
    98  			},
    99  			cleanup: func() {
   100  				require.NoError(t, os.Unsetenv("XDG_CONFIG_DIRS"))
   101  				xdg.Reload()
   102  			},
   103  		},
   104  	}
   105  	for _, test := range tests {
   106  		t.Run(test.name, func(t *testing.T) {
   107  			if test.cleanup != nil {
   108  				t.Cleanup(test.cleanup)
   109  			}
   110  			wd, err := os.Getwd()
   111  			require.NoError(t, err)
   112  
   113  			defer os.Chdir(wd) // reset working directory after test
   114  			application := &Application{}
   115  			viperInstance := viper.New()
   116  
   117  			// this will override home in case you are running this test locally and DO have a buildx config
   118  			// in your home directory... now it will be ignored. Same for XDG_CONFIG_DIRS.
   119  			t.Setenv("HOME", "/foo/bar")
   120  			t.Setenv("XDG_CONFIG_DIRS", "/foo/bar")
   121  			xdg.Reload()
   122  
   123  			configPath := test.setup(t)
   124  			err = application.LoadAllValues(viperInstance, configPath)
   125  			require.NoError(t, err)
   126  			test.assertions(t, application)
   127  		})
   128  	}
   129  }