github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/util/file_test.go (about)

     1  package util
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestBackupDirectoryExists(t *testing.T) {
    11  	tmpDir, err := ioutil.TempDir("", "ket-backupdir-test")
    12  	if err != nil {
    13  		t.Fatalf("Error creating temp dir: %v", err)
    14  	}
    15  	sourceDir := filepath.Join(tmpDir, ".helm")
    16  	err = os.Mkdir(sourceDir, 0755)
    17  	if err != nil {
    18  		t.Errorf("Expected error creating /tmp to be nil, got: %v", err)
    19  	}
    20  
    21  	exists, err := BackupDirectory(sourceDir, filepath.Join(tmpDir, ".helm.bak"))
    22  	if err != nil {
    23  		t.Errorf("Expected error to be nil, got: %v", err)
    24  	}
    25  	if !exists {
    26  		t.Errorf("Expected directory to exist")
    27  	}
    28  }
    29  
    30  func TestBackupClientDirectoryNotExists(t *testing.T) {
    31  	tmpDir, err := ioutil.TempDir("", "ket-backupdir-test")
    32  	if err != nil {
    33  		t.Fatalf("error creating temp dir: %v", err)
    34  	}
    35  	exists, err := BackupDirectory(filepath.Join(tmpDir, ".helm"), filepath.Join(tmpDir, ".helm.bak"))
    36  	if err != nil {
    37  		t.Errorf("Expected error to be nil, got: %v", err)
    38  	}
    39  	if exists {
    40  		t.Errorf("Expected directory to not exist")
    41  	}
    42  }