package configloader

import (
    "testing"
    "os"
    "path/filepath"
)

// ValidateUploadDir() should return an error if the entered directory
// does not exist.
func TestValidateUploadDirFailsNonExistentDir(t *testing.T) {

    returnVal, err := ValidateUploadDir("/this-directory-surely-does-not-exist")

    if err == nil {
        t.Fatalf("ValidateUploadDir() accepts non-existent directory")
    }
    if returnVal != "" {
        t.Fatalf("Return valid must be empty on failure")
    }

}

// Test to ensure that an error is thrown if a file is entered, not a directory.
func TestValidateUploadDirFailsWithDirContainingUnexpectedFiles(t *testing.T) {

    tmpDir := os.TempDir()
    os.OpenFile(tmpDir + "/empty-file-for-testing-that-dirs-are-not-accepted.txt", os.O_RDONLY|os.O_CREATE, 0666)

    returnVal, err := ValidateUploadDir(tmpDir + "/empty-file-for-testing-that-dirs-are-not-accepted.txt")

    if err == nil {
        t.Fatalf("ValidateUploadDir() accepts fails as upload directory")
    }
    if returnVal != "" {
        t.Fatalf("Return valid must be empty on failure")
    }

}

func TestValidateUploadDirSucceedsWithValidDir(t *testing.T) {

    tmpDir := os.TempDir()
    testDir := filepath.Join(tmpDir, "/existing-dir-for-import")
    mkdirErr := os.MkdirAll(testDir, os.ModePerm)

    if mkdirErr != nil {
        panic("Test failure: Failed to create test dir")
    }

    returnVal, err := ValidateUploadDir(tmpDir + "/existing-dir-for-import")

    if err != nil {
        t.Fatalf("ValidateUploadDir() fails with an existing, valid directory")
    }
    if returnVal == "" {
        t.Fatalf("Return valid must be empty on failure")
    }

}

