-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
avoid text file busy error on track #2494
Open
felipemadero
wants to merge
8
commits into
main
Choose a base branch
from
fix-text-file-busy-while-tracj
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+120
−22
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9119392
avoid text file busy error on track
felipemadero 081f826
Merge branch 'main' into fix-text-file-busy-while-tracj
felipemadero 7430f84
Merge branch 'main' into fix-text-file-busy-while-tracj
felipemadero 9116cf8
moved exec setup to utils and added unit
felipemadero 7f33e6d
lint
felipemadero 3751999
fix
felipemadero 0ee6c95
Merge branch 'main' into fix-text-file-busy-while-tracj
felipemadero 5d340a1
address PR comments
felipemadero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,11 @@ import ( | |
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanche-cli/pkg/constants" | ||
"github.com/ava-labs/avalanchego/utils/logging" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestExpandHome(t *testing.T) { | ||
|
@@ -45,29 +50,26 @@ func TestExpandHome(t *testing.T) { | |
} | ||
} | ||
|
||
// createTempGoMod creates a temporary go.mod file with the provided content. | ||
func createTempGoMod(t *testing.T, content string) string { | ||
// createTemp creates a temporary file with the provided name prefix and content. | ||
func createTemp(t *testing.T, namePrefix string, content string) string { | ||
t.Helper() | ||
file, err := os.CreateTemp("", "go.mod") | ||
file, err := os.CreateTemp("", namePrefix) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := file.Write([]byte(content)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := file.Close(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return file.Name() | ||
} | ||
|
||
// TestReadGoVersion tests all scenarios in one function using sub-tests. | ||
func TestReadGoVersion(t *testing.T) { | ||
t.Run("Success", func(t *testing.T) { | ||
tempFile := createTempGoMod(t, "module example.com/test\n\ngo 1.23\n") | ||
tempFile := createTemp(t, "go.mod", "module example.com/test\n\ngo 1.23\n") | ||
defer os.Remove(tempFile) // Clean up the temp file | ||
|
||
version, err := ReadGoVersion(tempFile) | ||
|
@@ -82,7 +84,7 @@ func TestReadGoVersion(t *testing.T) { | |
}) | ||
|
||
t.Run("NoVersion", func(t *testing.T) { | ||
tempFile := createTempGoMod(t, "module example.com/test\n") | ||
tempFile := createTemp(t, "go.mod", "module example.com/test\n") | ||
defer os.Remove(tempFile) | ||
|
||
_, err := ReadGoVersion(tempFile) | ||
|
@@ -98,3 +100,67 @@ func TestReadGoVersion(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestSetupExecFile(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love to see this test |
||
srcContent := "src content" | ||
destContent := "dest content" | ||
t.Run("Src does not exists", func(t *testing.T) { | ||
src := createTemp(t, "testexecfile", srcContent) | ||
dest := createTemp(t, "testexecfile", destContent) | ||
err := os.Remove(src) | ||
require.NoError(t, err) | ||
require.Equal(t, false, FileExists(src)) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, false, IsExecutable(dest)) | ||
err = SetupExecFile(logging.NoLog{}, src, dest) | ||
require.Error(t, err) | ||
content, err := os.ReadFile(dest) | ||
require.NoError(t, err) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, false, IsExecutable(dest)) | ||
require.Equal(t, destContent, string(content)) | ||
}) | ||
t.Run("Dest does not exists", func(t *testing.T) { | ||
src := createTemp(t, "testexecfile", srcContent) | ||
dest := createTemp(t, "testexecfile", destContent) | ||
err := os.Remove(dest) | ||
require.NoError(t, err) | ||
require.Equal(t, false, FileExists(dest)) | ||
require.Equal(t, false, IsExecutable(dest)) | ||
err = SetupExecFile(logging.NoLog{}, src, dest) | ||
require.NoError(t, err) | ||
content, err := os.ReadFile(dest) | ||
require.NoError(t, err) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, true, IsExecutable(dest)) | ||
require.Equal(t, srcContent, string(content)) | ||
}) | ||
t.Run("Dest is not executable", func(t *testing.T) { | ||
src := createTemp(t, "testexecfile", srcContent) | ||
dest := createTemp(t, "testexecfile", destContent) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, false, IsExecutable(dest)) | ||
err := SetupExecFile(logging.NoLog{}, src, dest) | ||
require.NoError(t, err) | ||
content, err := os.ReadFile(dest) | ||
require.NoError(t, err) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, true, IsExecutable(dest)) | ||
require.Equal(t, srcContent, string(content)) | ||
}) | ||
t.Run("Dest is already executable", func(t *testing.T) { | ||
src := createTemp(t, "testexecfile", srcContent) | ||
dest := createTemp(t, "testexecfile", destContent) | ||
err := os.Chmod(dest, constants.DefaultPerms755) | ||
require.NoError(t, err) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, true, IsExecutable(dest)) | ||
err = SetupExecFile(logging.NoLog{}, src, dest) | ||
require.NoError(t, err) | ||
content, err := os.ReadFile(dest) | ||
require.NoError(t, err) | ||
require.Equal(t, true, FileExists(dest)) | ||
require.Equal(t, true, IsExecutable(dest)) | ||
require.Equal(t, destContent, string(content)) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to error/log if the file isn't executable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to error. This also happens on first install.
We are not going to error on first install, and we prefer to try to
recover if a previous install failed.
Probably we can add log for a failed previous install, while trying to recover.