github.com/mattn/gom@v0.0.0-20190726063113-0ebf2b5d812d/gomfile_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func tempGomfile(content string) (string, error) {
    10  	f, err := ioutil.TempFile("", "gom")
    11  	if err != nil {
    12  		return "", err
    13  	}
    14  	defer f.Close()
    15  	_, err = f.WriteString(content)
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  	name := f.Name()
    20  	return name, nil
    21  }
    22  
    23  func TestGomfile1(t *testing.T) {
    24  	filename, err := tempGomfile(`
    25  gom 'github.com/mattn/go-sqlite3', :tag => '>3.33'
    26  `)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	goms, err := parseGomfile(filename)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	expected := []Gom{
    35  		{name: "github.com/mattn/go-sqlite3", options: map[string]interface{}{"tag": ">3.33"}},
    36  	}
    37  	if !reflect.DeepEqual(goms, expected) {
    38  		t.Fatalf("Expected %v, but %v:", expected, goms)
    39  	}
    40  }
    41  
    42  func TestGomfile2(t *testing.T) {
    43  	filename, err := tempGomfile(`
    44  gom 'github.com/mattn/go-sqlite3', :tag => '>3.33'
    45  gom 'github.com/mattn/go-gtk'
    46  `)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	goms, err := parseGomfile(filename)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	expected := []Gom{
    55  		{name: "github.com/mattn/go-sqlite3", options: map[string]interface{}{"tag": ">3.33"}},
    56  		{name: "github.com/mattn/go-gtk", options: map[string]interface{}{}},
    57  	}
    58  	if !reflect.DeepEqual(goms, expected) {
    59  		t.Fatalf("Expected %v, but %v:", expected, goms)
    60  	}
    61  }
    62  
    63  func TestGomfile3(t *testing.T) {
    64  	filename, err := tempGomfile(`
    65  gom 'github.com/mattn/go-sqlite3', :tag => '3.14', :commit => 'asdfasdf'
    66  gom 'github.com/mattn/go-gtk', :foobar => 'barbaz'
    67  `)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	goms, err := parseGomfile(filename)
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	expected := []Gom{
    76  		{name: "github.com/mattn/go-sqlite3", options: map[string]interface{}{"tag": "3.14", "commit": "asdfasdf"}},
    77  		{name: "github.com/mattn/go-gtk", options: map[string]interface{}{"foobar": "barbaz"}},
    78  	}
    79  	if !reflect.DeepEqual(goms, expected) {
    80  		t.Fatalf("Expected %v, but %v:", expected, goms)
    81  	}
    82  }
    83  
    84  func TestGomfile4(t *testing.T) {
    85  	filename, err := tempGomfile(`
    86  group :development do
    87  	gom 'github.com/mattn/go-sqlite3', :tag => '3.14', :commit => 'asdfasdf'
    88  end
    89  
    90  group :test do
    91  	gom 'github.com/mattn/go-gtk', :foobar => 'barbaz'
    92  end
    93  `)
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  
    98  	*developmentEnv = true
    99  	goms, err := parseGomfile(filename)
   100  	*developmentEnv = false
   101  
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  	expected := []Gom{
   106  		{name: "github.com/mattn/go-sqlite3", options: map[string]interface{}{"tag": "3.14", "commit": "asdfasdf", "group": []string{"development"}}},
   107  	}
   108  	if !reflect.DeepEqual(goms, expected) {
   109  		t.Fatalf("Expected %v, but %v:", expected, goms)
   110  	}
   111  }
   112  
   113  func TestGomfile5(t *testing.T) {
   114  	filename, err := tempGomfile(`
   115  group :custom_one do
   116  	gom 'github.com/mattn/go-sqlite3', :tag => '3.14', :commit => 'asdfasdf'
   117  end
   118  
   119  group :custom_two do
   120  	gom 'github.com/mattn/go-gtk', :foobar => 'barbaz'
   121  end
   122  `)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	customGroupList = []string{"custom_one", "custom_two"}
   128  	goms, err := parseGomfile(filename)
   129  
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  	expected := []Gom{
   134  		{name: "github.com/mattn/go-sqlite3", options: map[string]interface{}{"tag": "3.14", "commit": "asdfasdf", "group": []string{"custom_one"}}},
   135  		{name: "github.com/mattn/go-gtk", options: map[string]interface{}{"foobar": "barbaz", "group": []string{"custom_two"}}},
   136  	}
   137  	if !reflect.DeepEqual(goms, expected) {
   138  		t.Fatalf("Expected %v, but %v:", expected, goms)
   139  	}
   140  
   141  	customGroupList = []string{"custom_one"}
   142  	goms, err = parseGomfile(filename)
   143  
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	expected = []Gom{
   148  		{name: "github.com/mattn/go-sqlite3", options: map[string]interface{}{"tag": "3.14", "commit": "asdfasdf", "group": []string{"custom_one"}}},
   149  	}
   150  	if !reflect.DeepEqual(goms, expected) {
   151  		t.Fatalf("Expected %v, but %v:", expected, goms)
   152  	}
   153  }