Projects STRLCPY syft Commits 100cf100
🤬
Revision indexing in progress... (symbol navigation in revisions will be accurate after indexed)
  • ■ ■ ■ ■ ■ ■
    internal/config/application.go
    skipped 269 lines
    270 270   }
    271 271   }
    272 272   
    273  - // 4. look for .<appname>/config.yaml in xdg locations (starting with xdg home config dir, then moving upwards)
    274  - 
     273 + // 4. look for <appname>/config.yaml in xdg locations (starting with xdg home config dir, then moving upwards)
    275 274   v.SetConfigName("config")
    276  - configPath = path.Join(xdg.ConfigHome, "."+internal.ApplicationName)
     275 + configPath = path.Join(xdg.ConfigHome, internal.ApplicationName)
    277 276   v.AddConfigPath(configPath)
    278 277   for _, dir := range xdg.ConfigDirs {
    279  - v.AddConfigPath(path.Join(dir, "."+internal.ApplicationName))
     278 + v.AddConfigPath(path.Join(dir, internal.ApplicationName))
    280 279   }
    281 280   if err = v.ReadInConfig(); err == nil {
    282 281   v.Set("config", v.ConfigFileUsed())
    skipped 7 lines
  • ■ ■ ■ ■ ■ ■
    internal/config/application_test.go
    1 1  package config
    2 2   
    3 3  import (
     4 + "fmt"
    4 5   "os"
    5 6   "path"
    6 7   "testing"
    7 8   
    8 9   "github.com/adrg/xdg"
     10 + "github.com/mitchellh/go-homedir"
    9 11   "github.com/spf13/viper"
    10 12   "github.com/stretchr/testify/assert"
     13 + "github.com/stretchr/testify/require"
    11 14  )
    12 15   
    13 16  // TODO: set negative case when config.yaml is no longer a valid option
    14 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 + 
    15 33   // config is picked up at desired configuration paths
    16 34   // VALID: .syft.yaml, .syft/config.yaml, ~/.syft.yaml, <XDG_CONFIG_HOME>/syft/config.yaml
    17 35   // DEPRECATED: config.yaml is currently supported by
    skipped 1 lines
    19 37   name string
    20 38   setup func(t *testing.T) string
    21 39   assertions func(t *testing.T, app *Application)
    22  - Cleanup func(t *testing.T)
     40 + cleanup func()
    23 41   }{
    24 42   {
    25 43   name: "explicit config",
    skipped 3 lines
    29 47   assertions: func(t *testing.T, app *Application) {
    30 48   assert.Equal(t, "test-explicit-config", app.File)
    31 49   },
    32  - Cleanup: func(t *testing.T) {},
    33 50   },
    34 51   {
    35 52   name: "current working directory named config",
    36 53   setup: func(t *testing.T) string {
    37 54   err := os.Chdir("./test-fixtures/config-wd-file") // change application cwd to test-fixtures
    38  - if err != nil {
    39  - t.Fatalf("%s failed to change cwd: %+v", t.Name(), err)
    40  - }
     55 + require.NoError(t, err)
    41 56   return ""
    42 57   },
    43 58   assertions: func(t *testing.T, app *Application) {
    44 59   assert.Equal(t, "test-wd-named-config", app.File)
    45 60   },
    46  - Cleanup: func(t *testing.T) {},
    47 61   },
    48 62   {
    49 63   name: "current working directory syft dir config",
    50 64   setup: func(t *testing.T) string {
    51 65   err := os.Chdir("./test-fixtures/config-dir-test") // change application cwd to test-fixtures
    52  - if err != nil {
    53  - t.Fatalf("%s failed to change cwd: %+v", t.Name(), err)
    54  - }
     66 + require.NoError(t, err)
    55 67   return ""
    56 68   },
    57 69   assertions: func(t *testing.T, app *Application) {
    58 70   assert.Equal(t, "test-dir-config", app.File)
    59 71   },
    60  - Cleanup: func(t *testing.T) {},
    61 72   },
    62 73   {
    63 74   name: "home directory file config",
    64 75   setup: func(t *testing.T) string {
    65 76   // Because Setenv affects the whole process, it cannot be used in parallel tests or
    66 77   // tests with parallel ancestors: see separate XDG test for consequence of this
    67  - t.Setenv("HOME", "./test-fixtures/config-home-test")
    68  - err := os.Link("./test-fixtures/config-home-test/config-file/.syft.yaml", "./test-fixtures/config-home-test/.syft.yaml")
    69  - if err != nil {
    70  - t.Fatalf("%s failed to link home config: %+v", t.Name(), err)
    71  - }
     78 + t.Setenv("HOME", "./test-fixtures/config-home-test/config-file")
    72 79   return ""
    73 80   },
    74 81   assertions: func(t *testing.T, app *Application) {
    75 82   assert.Equal(t, "test-home-config", app.File)
    76 83   },
    77  - Cleanup: func(t *testing.T) {
    78  - err := os.Remove("./test-fixtures/config-home-test/.syft.yaml") //
    79  - if err != nil {
    80  - t.Fatalf("%s failed to remove home config link: %+v", t.Name(), err)
    81  - }
    82  - },
    83 84   },
    84 85   {
    85 86   name: "XDG file config",
    86 87   setup: func(t *testing.T) string {
    87 88   wd, err := os.Getwd()
    88  - if err != nil {
    89  - t.Fatalf("%s: failed to get working directory: %+v", t.Name(), err)
    90  - }
     89 + require.NoError(t, err)
    91 90   configDir := path.Join(wd, "./test-fixtures/config-home-test") // set HOME to testdata
    92  - t.Setenv("XDG_CONFIG_DIRS", configDir)
     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 93   xdg.Reload()
    94 94   return ""
    95 95   },
    96 96   assertions: func(t *testing.T, app *Application) {
    97 97   assert.Equal(t, "test-home-XDG-config", app.File)
    98 98   },
    99  - Cleanup: func(t *testing.T) {},
     99 + cleanup: func() {
     100 + require.NoError(t, os.Unsetenv("XDG_CONFIG_DIRS"))
     101 + xdg.Reload()
     102 + },
    100 103   },
    101 104   }
    102 105   for _, test := range tests {
    103 106   t.Run(test.name, func(t *testing.T) {
    104  - defer test.Cleanup(t)
     107 + if test.cleanup != nil {
     108 + t.Cleanup(test.cleanup)
     109 + }
    105 110   wd, err := os.Getwd()
    106  - if err != nil {
    107  - t.Fatalf("failed to get working directory: %+v", err)
    108  - }
     111 + require.NoError(t, err)
     112 + 
    109 113   defer os.Chdir(wd) // reset working directory after test
    110 114   application := &Application{}
    111 115   viperInstance := viper.New()
    112 116   
     117 + // this will override home in case you are running this test locally and DO have a syft 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 + 
    113 123   configPath := test.setup(t)
    114 124   err = application.LoadAllValues(viperInstance, configPath)
    115  - if err != nil {
    116  - t.Fatalf("failed to load application config: %+v", err)
    117  - }
     125 + require.NoError(t, err)
    118 126   test.assertions(t, application)
    119 127   })
    120 128   }
    skipped 2 lines
  • internal/config/test-fixtures/config-home-test/.syft/config.yaml internal/config/test-fixtures/config-home-test/syft/config.yaml
    Content is identical
Please wait...
Page is in error, reload to recover