2018-03-17 12:36:30 +01:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-08-28 17:07:36 +02:00
|
|
|
"runtime"
|
2018-03-17 12:36:30 +01:00
|
|
|
"testing"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs/config"
|
2018-03-17 12:36:30 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVersionWorksWithoutAccessibleConfigFile(t *testing.T) {
|
|
|
|
// create temp config file
|
2022-08-20 16:38:02 +02:00
|
|
|
tempFile, err := os.CreateTemp("", "unreadable_config.conf")
|
2018-03-17 12:36:30 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
path := tempFile.Name()
|
|
|
|
defer func() {
|
|
|
|
err := os.Remove(path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}()
|
|
|
|
assert.NoError(t, tempFile.Close())
|
2018-08-28 17:07:36 +02:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
assert.NoError(t, os.Chmod(path, 0000))
|
|
|
|
}
|
2018-03-17 12:36:30 +01:00
|
|
|
// re-wire
|
|
|
|
oldOsStdout := os.Stdout
|
2021-04-08 17:49:47 +02:00
|
|
|
oldConfigPath := config.GetConfigPath()
|
|
|
|
assert.NoError(t, config.SetConfigPath(path))
|
2018-03-17 12:36:30 +01:00
|
|
|
os.Stdout = nil
|
|
|
|
defer func() {
|
|
|
|
os.Stdout = oldOsStdout
|
2021-04-08 17:49:47 +02:00
|
|
|
assert.NoError(t, config.SetConfigPath(oldConfigPath))
|
2018-03-17 12:36:30 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
cmd.Root.SetArgs([]string{"version"})
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
assert.NoError(t, cmd.Root.Execute())
|
|
|
|
})
|
|
|
|
|
2018-06-15 17:22:59 +02:00
|
|
|
// This causes rclone to exit and the tests to stop!
|
|
|
|
// cmd.Root.SetArgs([]string{"--version"})
|
|
|
|
// assert.NotPanics(t, func() {
|
|
|
|
// assert.NoError(t, cmd.Root.Execute())
|
|
|
|
// })
|
|
|
|
}
|