-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed problems with copying the data folder for nested remote filters…
…, added test for this case. Close #32
- Loading branch information
Showing
24 changed files
with
348 additions
and
91 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package test | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"io/fs" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
const freshProjectPath = "testdata/fresh_project" | ||
const minimalProjectPath = "testdata/minimal_project" | ||
const multitargetProjectPath = "testdata/multitarget_project" | ||
const doubleRemoteProjectPath = "testdata/double_remote_project" | ||
const doubleRemoteProjectInstalledPath = "testdata/double_remote_project_installed" | ||
|
||
// listPaths returns a dictionary with paths of the files from 'path' directory | ||
// relative to 'root' directory used as keys, and with md5 hashes paths as | ||
// values. The directory paths use empty strings instead of MD5. The function | ||
// ignores files called .ignoreme (they simulate empty directories | ||
// in git repository). | ||
func listPaths(path string, root string) (map[string]string, error) { | ||
result := map[string]string{} | ||
err := filepath.WalkDir(path, | ||
func(path string, data fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if data.Name() == ".ignoreme" { // Ignored file | ||
return nil | ||
} | ||
relPath, err := filepath.Rel(root, path) | ||
if err != nil { | ||
return err | ||
} | ||
if data.IsDir() { | ||
result[relPath] = "" | ||
} else { | ||
content, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
hash := md5.New() | ||
// Get the hash value, ignore carriage return | ||
hash.Write([]byte(strings.Replace(string(content), "\r", "", -1))) | ||
hashInBytes := hash.Sum(nil) | ||
result[relPath] = hex.EncodeToString(hashInBytes) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return map[string]string{}, err | ||
} | ||
return result, nil | ||
} | ||
|
||
// comparePathMaps compares maps created by listPaths function and runs | ||
// t.Fatal in case of finding a difference. | ||
func comparePathMaps( | ||
expectedPaths map[string]string, createdPaths map[string]string, | ||
t *testing.T, | ||
) { | ||
checked := struct{}{} | ||
checklist := map[string]struct{}{} | ||
// Check if all expectedPaths are created | ||
for k, expectedHash := range expectedPaths { | ||
checklist[k] = checked | ||
createdHash, exists := createdPaths[k] | ||
if !exists { | ||
t.Fatal("Missing expected path:", k) | ||
} else if createdHash != expectedHash { | ||
if expectedHash == "" { | ||
t.Fatalf("%q should be a file but is a directory instead", k) | ||
} else if createdHash == "" { | ||
t.Fatalf("%q should be a directory but is a file instead", k) | ||
} | ||
// Print the file, that doesn't match | ||
//bytes, _ := ioutil.ReadFile(k) | ||
//t.Log(string(bytes)) | ||
t.Fatalf("%q file is different that expected", k) | ||
} | ||
} | ||
// Check if all createdPaths are expected | ||
for k, createdHash := range createdPaths { | ||
if _, checked := checklist[k]; checked { | ||
continue // This is checked already (skip) | ||
} | ||
expectedHash, exists := expectedPaths[k] | ||
if !exists { | ||
t.Fatal("Additional unexpected path was created:", k) | ||
} else if createdHash != expectedHash { | ||
if expectedHash == "" { | ||
t.Fatalf("%q should be a file but is a directory instead", k) | ||
} else if createdHash == "" { | ||
t.Fatalf("%q should be a directory but is a file instead", k) | ||
} | ||
t.Fatalf("%q file is different that expected", k) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package test | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"bedrock-oss.github.com/regolith/regolith" | ||
"github.com/otiai10/copy" | ||
) | ||
|
||
// TestDoubleRemoteFilter tests how regolith handles installing a remote | ||
// with dependencies that point at another remote filter. | ||
func TestDoubleRemoteFilter(t *testing.T) { | ||
// Switching working directories in this test, make sure to go back | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal("Unable to get current working directory") | ||
} | ||
defer os.Chdir(wd) | ||
// Load expected output | ||
expectedPaths, err := listPaths( | ||
doubleRemoteProjectInstalledPath, doubleRemoteProjectInstalledPath) | ||
if err != nil { | ||
t.Fatal("Unable load the expected paths:", err) | ||
} | ||
// Create a temporary directory | ||
tmpDir, err := ioutil.TempDir("", "regolith-test") | ||
if err != nil { | ||
t.Fatal("Unable to create temporary directory:", err) | ||
} | ||
t.Log("Created temporary directory:", tmpDir) | ||
// Before deleting "workingDir" the test must stop using it | ||
defer os.RemoveAll(tmpDir) | ||
defer os.Chdir(wd) | ||
workingDir := filepath.Join(tmpDir, "working-dir") | ||
os.Mkdir(workingDir, 0666) | ||
// Copy the test project to the working directory | ||
err = copy.Copy( | ||
doubleRemoteProjectPath, | ||
workingDir, | ||
copy.Options{PreserveTimes: false, Sync: false}, | ||
) | ||
if err != nil { | ||
t.Fatalf( | ||
"Failed to copy test files %q into the working directory %q", | ||
doubleRemoteProjectPath, workingDir, | ||
) | ||
} | ||
// Switch to the working directory | ||
os.Chdir(workingDir) | ||
// Run InstallDependencies | ||
regolith.InitLogging(true) | ||
regolith.RegisterFilters() | ||
regolith.InstallDependencies(false) | ||
// Load created paths for comparison with expected output | ||
createdPaths, err := listPaths(".", ".") | ||
if err != nil { | ||
t.Fatal("Unable to load the created paths:", err) | ||
} | ||
// Compare the installed dependencies with the expected dependencies | ||
comparePathMaps(expectedPaths, createdPaths, t) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/build | ||
/.regolith |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "regolith_test_project", | ||
"author": "Bedrock-OSS", | ||
"packs": { | ||
"behaviorPack": "./packs/BP", | ||
"resourcePack": "./packs/RP" | ||
}, | ||
"regolith": { | ||
"profiles": { | ||
"dev": { | ||
"unsafe": false, | ||
"filters": [ | ||
{ | ||
"url": "github.com/Bedrock-OSS/regolith-test-filters/nested-remote-filter" | ||
} | ||
], | ||
"export": { | ||
"target": "development" | ||
}, | ||
"dataPath": "./packs/data" | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/testdata/double_remote_project/packs/BP/manifest.json
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"format_version": 2, | ||
"header": { | ||
"description": "This is test BP", | ||
"name": "Regolith Test BP", | ||
"uuid": "96b53fd2-b7a1-4d26-b74f-1b9394c8d0bc", | ||
"version": [1, 0, 0], | ||
"min_engine_version": [1, 16, 0] | ||
}, | ||
"modules": [ | ||
{ | ||
"type": "data", | ||
"uuid": "4eef1f3f-91b5-43df-b5ab-07e9aa89081b", | ||
"version": [1, 0, 0] | ||
} | ||
], | ||
"dependencies": [ | ||
{ | ||
"uuid": "6f6e3f0b-1627-488d-a9aa-2d1430ba368a", | ||
"version": [1, 0, 0] | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
test/testdata/double_remote_project/packs/RP/manifest.json
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"format_version": 2, | ||
"header": { | ||
"description": "This is test RP", | ||
"name": "Regolith Test RP", | ||
"uuid": "6f6e3f0b-1627-488d-a9aa-2d1430ba368a", | ||
"version": [1, 0, 0], | ||
"min_engine_version": [1, 16, 0] | ||
}, | ||
"modules": [ | ||
{ | ||
"type": "resources", | ||
"uuid": "65b1ba69-462d-4199-aa3b-a0f161ed0bde", | ||
"version": [1, 0, 0] | ||
} | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
test/testdata/double_remote_project/packs/data/example_data_file.json
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/build | ||
/.regolith |
Oops, something went wrong.