Skip to content

Commit

Permalink
ascend all parent dirs looking for files
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrunde committed Jul 13, 2023
1 parent aa0364b commit f06a4d3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
65 changes: 26 additions & 39 deletions app/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,18 +572,10 @@ func copyDockerPaths(target string, dockerPaths []string) (err error) {

// when the directory does not exist, keep moving back in the directory structure until it is found
if os.IsNotExist(err) {
newDirPath := ""
parent := "../" + dirPath
for i := 1; i <= 5; i++ {
f, parentExists := os.Stat(parent)
if parentExists == nil && f.IsDir() {
newDirPath = parent
break
} else if parentExists == nil && !f.IsDir() {
return fmt.Errorf("docker_directory_path %s is not a directory", dirPath)
} else {
parent = "../" + parent
}
fileInfo, newDirPath := fileFromParents(dirPath)

if fileInfo != nil && !fileInfo.IsDir() {
return fmt.Errorf("docker_directory_path %s is not a directory", dirPath)
}

if newDirPath != "" {
Expand Down Expand Up @@ -629,40 +621,15 @@ func copyChallengePaths(target string, challengePaths []string) (err error) {
return err
}

// lost in the file system
// /dir/foo/file.md

// * test_path: /dir/foo/test.js
// ../dir/foo/test.js
// ../../dir/foo/test.js <- find here
// ../../../dir/foo/test.js
// ../../../../dir/foo/test.js
// */*../../../../dir/foo/test.js >> os.Stat

// os.Stat("../../") isDir && fileName == "/"

if strings.HasPrefix(filePath, "/") {
filePath = trimFirstRune(filePath)
}
// -> "my_neat_test.js"
fileName := pathArray[len(pathArray)-1]

// use the full filePath as challenge paths start with a slash and begin at project root.
// use the filePath as challenge paths start with a slash and begin at project root.
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// Here we go back a directory at least 5 times trying to find the root of the project repo
// resource paths like 'data_path' are always from the root of the project, never relative.
// The file can be found if we keep checking for its existence, stepping back a dir when not found
useThisPath := ""
parent := "../" + filePath
for i := 1; i <= 5; i++ {
_, parentExists := os.Stat(parent)
if parentExists == nil {
useThisPath = parent
break
} else {
parent = "../" + parent
}
}
_, useThisPath := fileFromParents(filePath)

if useThisPath != "" {
err = Copy(useThisPath, tmpSingleFileDir+linkDirs+"/"+fileName)
Expand Down Expand Up @@ -926,6 +893,26 @@ func CopyDirectoryContents(src, dst string, ignorePatterns []string) error {
return nil
}

// fileFromParents checks for a file from the given filePath, and if not found it will
// move up a parent and check again. Checking stops once the root of the file system is hit
func fileFromParents(filePath string) (file os.FileInfo, path string) {
dotdotSlashes := "../"
priorDir, _ := filepath.Abs(dotdotSlashes)
for priorDir != "/" {
file, parentExists := os.Stat(dotdotSlashes + filePath)
if parentExists == nil {
path = dotdotSlashes + filePath
file = file
break
} else {
dotdotSlashes = "../" + dotdotSlashes
priorDir, _ = filepath.Abs(dotdotSlashes)
}
}

return file, path
}

func trimFirstRune(s string) string {
for i := range s {
if i > 0 {
Expand Down
24 changes: 12 additions & 12 deletions app/cmd/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func Test_compressDirectory(t *testing.T) {

tmpZipFile := "../../fixtures/test-block-auto-config/preview-curriculum.zip"

var resourcePaths []string
resourcePaths = append(resourcePaths, "test-block-auto-config/docker/text.text")
resourcePaths = append(resourcePaths, "test-block-auto-config/sql/database.sql")
var challengePaths []string
challengePaths = append(challengePaths, "test-block-auto-config/docker/text.text")
challengePaths = append(challengePaths, "test-block-auto-config/sql/database.sql")

previewer := previewBuilder{
target: source,
resourcePaths: resourcePaths,
challengePaths: challengePaths,
configYamlPaths: configYamlPaths,
}
err = previewer.compressDirectory(tmpZipFile)
Expand All @@ -80,7 +80,7 @@ func Test_compressDirectory(t *testing.T) {
paths[path] = true
}
}
for _, includedPath := range resourcePaths {
for _, includedPath := range challengePaths {
if strings.Contains(includedPath, path) {
paths[path] = true
}
Expand All @@ -100,7 +100,7 @@ func Test_compressDirectory(t *testing.T) {
}

func Test_createNewTarget(t *testing.T) {
result, err := createNewTarget("../../fixtures/test-links/nested/test.md", []string{"./mrsmall-invert.png", "../mrsmall.png", "../image/nested-small.png", "deeper/deep-small.png"}, []string{})
result, err := createNewTarget("../../fixtures/test-links/nested/test.md", []string{}, []string{"./mrsmall-invert.png", "../mrsmall.png", "../image/nested-small.png", "deeper/deep-small.png"}, []string{})
if err != nil {
t.Errorf("Attempting to createNewTarget errored: %s\n", err)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func Test_createNewTargetSingleFileSQLWithImage(t *testing.T) {
}

output := captureOutput(func() {
createNewTarget("test.md", []string{"/data/some.sql", "image/nested-small.png"}, []string{})
createNewTarget("test.md", []string{"/data/some.sql"}, []string{"image/nested-small.png"}, []string{})
_, err := os.Stat(fmt.Sprintf("single-file-upload/%s", "data/some.sql"))
if err == nil {
t.Errorf("data/some.sql should have been copied over and it was not")
Expand Down Expand Up @@ -192,7 +192,7 @@ func Test_createNewTargetSingleFileThatIsSQL(t *testing.T) {
t.Errorf("Error creating test.md: %s\n", err)
}
output := captureOutput(func() {
createNewTarget("test.md", []string{"/data/some.sql"}, []string{})
createNewTarget("test.md", []string{"/data/some.sql"}, []string{}, []string{})
_, err := os.Stat(fmt.Sprintf("single-file-upload/%s", "data/some.sql"))
if err == nil {
t.Errorf("data/some.sql should have been copied over and it was not")
Expand All @@ -219,7 +219,7 @@ func Test_createNewTargetSingleFile(t *testing.T) {
}

output := captureOutput(func() {
result, err := createNewTarget("test.md", []string{"./image/nested-small.png", "image/nested-small.png", "../nested-small.png"}, []string{})
result, err := createNewTarget("test.md", []string{}, []string{"./image/nested-small.png", "image/nested-small.png", "../nested-small.png"}, []string{})
if err != nil {
t.Errorf("Attempting to createNewTarget errored: %s\n", err)
}
Expand Down Expand Up @@ -323,7 +323,7 @@ func Test_createNewTarget_DockerDirectoryIgnore(t *testing.T) {
ignoreFile.Write([]byte("docker-compose.yml"))

output := captureOutput(func() {
result, err := createNewTarget("test.md", []string{}, []string{"/path/to/dir"})
result, err := createNewTarget("test.md", []string{}, []string{}, []string{"/path/to/dir"})
if err != nil {
t.Errorf("Attempting to createNewTarget errored: %s\n", err)
}
Expand Down Expand Up @@ -403,7 +403,7 @@ func Test_createNewTarget_DockerDirectoryNestedMd(t *testing.T) {
}

output := captureOutput(func() {
result, err := createNewTarget("test.md", []string{}, []string{"/path/to/dir"})
result, err := createNewTarget("test.md", []string{}, []string{}, []string{"/path/to/dir"})
if err != nil {
t.Errorf("Attempting to createNewTarget errored: %s\n", err)
}
Expand Down Expand Up @@ -461,7 +461,7 @@ func Test_createNewTarget_DockerDirectoryDoubleNestedMd(t *testing.T) {
}

output := captureOutput(func() {
result, err := createNewTarget("test.md", []string{}, []string{"/path/to/dir"})
result, err := createNewTarget("test.md", []string{}, []string{}, []string{"/path/to/dir"})
if err != nil {
t.Errorf("Attempting to createNewTarget errored: %s\n", err)
}
Expand Down

0 comments on commit f06a4d3

Please sign in to comment.