github.com/erroneousboat/dot@v0.0.0-20170402193747-d2fd504d98ec/config_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"syscall"
     8  	"testing"
     9  )
    10  
    11  const payload string = `
    12  {
    13  	"dot_path": "/path/to/dotfiles",
    14  	"files": {
    15  		"test_file_1": "path-to-test-file-1",
    16  		"test_file_2": "path-to-test-file-2"
    17  	}
    18  }
    19  `
    20  
    21  // Define here the setup and teardown functions
    22  func TestMain(m *testing.M) {
    23  	setUp()
    24  	retCode := m.Run()
    25  	os.Exit(retCode)
    26  }
    27  
    28  // setUp() will create a config file for the other tests so we can test the
    29  // methods associated with the config struct
    30  func setUp() {
    31  	// create a test dotconfig file
    32  	f, err := ioutil.TempFile("", ".dotconfig")
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  	defer syscall.Unlink(f.Name())
    37  
    38  	ioutil.WriteFile(
    39  		f.Name(),
    40  		[]byte(payload),
    41  		0644,
    42  	)
    43  
    44  	// try to read the file
    45  	_, err = NewConfig(f.Name())
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  }
    50  
    51  func TestNewConfig(t *testing.T) {
    52  	// create a test dotconfig file
    53  	f, err := ioutil.TempFile("", ".dotconfig")
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	defer syscall.Unlink(f.Name())
    58  
    59  	ioutil.WriteFile(
    60  		f.Name(),
    61  		[]byte(payload),
    62  		0644,
    63  	)
    64  
    65  	// try to read the file
    66  	c, err := NewConfig(f.Name())
    67  	if err != nil {
    68  		log.Fatal(err)
    69  	}
    70  
    71  	// c.DotPath should be the same as payload
    72  	if c.DotPath != "/path/to/dotfiles" {
    73  		t.Error("c.DotPath doesn't match")
    74  	}
    75  
    76  	// c.Files should be the same as payload
    77  	if c.Files["test_file_1"] != "path-to-test-file-1" {
    78  		t.Error("c.Files doesn't match")
    79  	}
    80  
    81  	// c.Files should be the same as payload
    82  	if c.Files["test_file_2"] != "path-to-test-file-2" {
    83  		t.Error("c.Files doesn't match")
    84  	}
    85  }