Skip to content

Commit 4e0f765

Browse files
Remove latest_version.txt creation in user curr dir (#3179)
* fix version text in current dir * empty dir check * version checker test * moved init out of version check * fix comment --------- Co-authored-by: Gauri Lamunion <[email protected]>
1 parent 42c8c03 commit 4e0f765

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

cmd/root.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ var rootCmd = &cobra.Command{
7676
Use: "azcopy",
7777
Short: rootCmdShortDescription,
7878
Long: rootCmdLongDescription,
79+
// PersistentPreRunE hook will not run on just `azcopy` without any subcommand
7980
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
8081
glcm.RegisterCloseFunc(func() {
8182
if debugMemoryProfile != "" {
@@ -262,6 +263,14 @@ func InitializeAndExecute() {
262263
glcm.Error(err.Error())
263264
} else {
264265
if !SkipVersionCheck && !isPipeDownload {
266+
// In commands like azcopy --version and azcopy --help, they do not reach Initialize() in the PersistentPreRunE
267+
// So we set the log path folder here.
268+
// This ensures we sure we don't write to user's current directory
269+
if common.LogPathFolder == "" {
270+
// With this, latest_version.txt is written to app dir
271+
common.InitializeFolders()
272+
}
273+
265274
// our commands all control their own life explicitly with the lifecycle manager
266275
// only commands that don't explicitly exit actually reach this point (e.g. help commands)
267276
select {
@@ -345,7 +354,6 @@ func beginDetectNewVersion() chan struct{} {
345354
if err != nil {
346355
return
347356
}
348-
349357
// Step 1: Fetch & validate cached version. If it is up to date, we return without making API calls
350358
filePath := filepath.Join(common.LogPathFolder, "latest_version.txt")
351359
cachedVersion, err := ValidateCachedVersion(filePath) // same as the remote version

cmd/versionChecker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func (v Version) EqualTo(v2 Version) bool {
121121
func (v Version) CacheRemoteVersion(remoteVer Version, filePath string) error {
122122
if v.OlderThan(remoteVer) || v.EqualTo(remoteVer) {
123123
expiry := time.Now().Add(24 * time.Hour).Format(versionFileTimeFormat)
124+
// make sure filepath is absolute filepath so WriteFile is not written to customers current directory
124125
if err := os.WriteFile(filePath, []byte(remoteVer.original+","+expiry), 0666); err != nil {
125126
return err
126127
}

e2etest/zt_version_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
package e2etest
2121

2222
import (
23+
cmd2 "github.com/Azure/azure-storage-azcopy/v10/testSuite/cmd"
24+
"github.com/stretchr/testify/assert"
25+
"os"
2326
"os/exec"
27+
"path/filepath"
2428
"regexp"
2529
"strings"
2630
"testing"
31+
"time"
2732

2833
"github.com/Azure/azure-storage-azcopy/v10/common"
2934
)
@@ -68,3 +73,46 @@ func TestVersionCommand(t *testing.T) {
6873
t.FailNow()
6974
}
7075
}
76+
77+
// Test that latest_version file is uploaded to correct directory
78+
func TestLastVersionFileLocation(t *testing.T) {
79+
a := assert.New(t)
80+
81+
// Save original working directory
82+
originalWorkDir, err := os.Getwd()
83+
a.NoError(err)
84+
85+
defer func() { // Reset current dir after test
86+
os.Chdir(originalWorkDir)
87+
}()
88+
89+
// Create temp dir to use as current working directory
90+
tempWorkDir, err := os.MkdirTemp("", "temp")
91+
a.NoError(err)
92+
defer func() { // Clean up
93+
os.RemoveAll(tempWorkDir)
94+
}()
95+
96+
// Run the help command
97+
cmd := exec.Command(GlobalInputManager{}.GetExecutablePath(), "--help")
98+
output, err := cmd.CombinedOutput()
99+
a.NoError(err, "Help command should work")
100+
a.NotEmpty(output)
101+
102+
// Check that latest_version.txt is NOT in current directory
103+
currDirFile := filepath.Join(tempWorkDir, "latest_version.txt")
104+
_, err = os.Stat(currDirFile)
105+
a.True(os.IsNotExist(err), "latest_version.txt should NOT be in current directory")
106+
107+
// The file should be in the app's log directory
108+
appDataFolder := cmd2.GetAzCopyAppPath()
109+
if appDataFolder != "" {
110+
expectedFile := filepath.Join(appDataFolder, "latest_version.txt")
111+
time.Sleep(2 * time.Second) // Wait for version check to complete
112+
113+
// Check if file exists
114+
if _, err := os.Stat(expectedFile); err == nil {
115+
a.True(true, "latest_version.txt found in app dir")
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)