github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/version/version_test.go (about)

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