version_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package version
  2. import (
  3. "os"
  4. "runtime"
  5. "testing"
  6. "github.com/rclone/rclone/cmd"
  7. "github.com/rclone/rclone/fs/config"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestVersionWorksWithoutAccessibleConfigFile(t *testing.T) {
  11. // create temp config file
  12. tempFile, err := os.CreateTemp("", "unreadable_config.conf")
  13. assert.NoError(t, err)
  14. path := tempFile.Name()
  15. defer func() {
  16. err := os.Remove(path)
  17. assert.NoError(t, err)
  18. }()
  19. assert.NoError(t, tempFile.Close())
  20. if runtime.GOOS != "windows" {
  21. assert.NoError(t, os.Chmod(path, 0000))
  22. }
  23. // re-wire
  24. oldOsStdout := os.Stdout
  25. oldConfigPath := config.GetConfigPath()
  26. assert.NoError(t, config.SetConfigPath(path))
  27. os.Stdout = nil
  28. defer func() {
  29. os.Stdout = oldOsStdout
  30. assert.NoError(t, config.SetConfigPath(oldConfigPath))
  31. }()
  32. cmd.Root.SetArgs([]string{"version"})
  33. assert.NotPanics(t, func() {
  34. assert.NoError(t, cmd.Root.Execute())
  35. })
  36. // This causes rclone to exit and the tests to stop!
  37. // cmd.Root.SetArgs([]string{"--version"})
  38. // assert.NotPanics(t, func() {
  39. // assert.NoError(t, cmd.Root.Execute())
  40. // })
  41. }