Skip to content

Use require package instead of assert while performing fs operations in unit tests #90

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
543e173
Changed TestChain to use core.Message rather than types.Message
Xenomega Dec 11, 2022
65c470f
Temporary push in between merging #73
Xenomega Dec 30, 2022
1218e28
Merge branch 'master' into dev/call-sequence-mutations
Xenomega Jan 3, 2023
2a9d74a
Temp push between re-organization of types and mutation introduction
Xenomega Jan 4, 2023
3f20462
Commit prior to CallMessage deep cloning + actual sequence mutations
Xenomega Jan 5, 2023
6f62f65
Moved all existing new call sequence generation code to the CallSeque…
Xenomega Jan 6, 2023
4491778
Introduction of call sequence mutations with head/tail building funct…
Xenomega Jan 9, 2023
2502135
* Change contract deployments to use full block gas limit rather than…
Xenomega Jan 9, 2023
1ae4c24
* Added clone method to CallMessage and CallMessageDataAbiValues so c…
Xenomega Jan 12, 2023
e478189
Added tests for ABI value mutation
Xenomega Jan 12, 2023
bb0a06f
Added array/slice structure mutation methods to ValueGenerator
Xenomega Jan 13, 2023
4f4f41e
Fixed a mistake made when updating integer mutation internal helper m…
Xenomega Jan 14, 2023
df555b8
Moved CallSequenceGenerator weights to a runtime config
Xenomega Jan 17, 2023
3fe68a5
Fix CallMessage field JSON key to be camel case, added error handling…
Xenomega Jan 19, 2023
063ca6d
Updated corpus to fix an issue where the wrong error variable would b…
Xenomega Jan 19, 2023
b94eac2
initial testutils update
ahpaleus Jan 20, 2023
cb5f91d
Merge remote-tracking branch 'origin/master' into dev/require-instead…
Xenomega Jan 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions utils/testutils/test_directory_utils.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package testutils

import (
"github.com/stretchr/testify/assert"
"github.com/trailofbits/medusa/utils"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"github.com/trailofbits/medusa/utils"
)

// CopyToTestDirectory copies files or directories from the provided filePath (relative to ./tests/contracts/) to an
// ephemeral directory used for unit tests.
func CopyToTestDirectory(t *testing.T, filePath string) string {
// Construct our file path relative to our working directory
cwd, err := os.Getwd()
assert.NoError(t, err)
require.NoError(t, err)
sourcePath := filepath.Join(cwd, filePath)

// Verify the file path exists
sourcePathInfo, err := os.Stat(sourcePath)
assert.False(t, os.IsNotExist(err))
assert.NotNil(t, sourcePathInfo)
require.False(t, os.IsNotExist(err))
require.NotNil(t, sourcePathInfo)

// Obtain an isolated test directory path.
targetDirectory := filepath.Join(t.TempDir(), "medusaTest")
Expand All @@ -30,11 +31,11 @@ func CopyToTestDirectory(t *testing.T, filePath string) string {
} else {
err = utils.CopyFile(sourcePath, targetPath)
}
assert.NoError(t, err)
require.NoError(t, err)

// Get a normalized absolute path
targetPath, err = filepath.Abs(targetPath)
assert.NoError(t, err)
require.NoError(t, err)
return targetPath
}

Expand All @@ -44,12 +45,12 @@ func CopyToTestDirectory(t *testing.T, filePath string) string {
func ExecuteInDirectory(t *testing.T, testPath string, method func()) {
// Backup our old working directory
cwd, err := os.Getwd()
assert.NoError(t, err)
require.NoError(t, err)

// Check if the test path refers to a file or directory, as we'll want to change our working directory to a
// directory path.
testPathInfo, err := os.Stat(testPath)
assert.NoError(t, err)
require.NoError(t, err)

// Ensure we obtained a directory from our path
testDirectory := testPath
Expand All @@ -59,12 +60,12 @@ func ExecuteInDirectory(t *testing.T, testPath string, method func()) {

// Change our working directory to the test directory
err = os.Chdir(testDirectory)
assert.NoError(t, err)
require.NoError(t, err)

// Execute the given method
method()

// Restore our working directory (we must leave the test directory or else clean up will fail post testing)
err = os.Chdir(cwd)
assert.NoError(t, err)
require.NoError(t, err)
}