github.com/echohead/hub@v2.2.1+incompatible/github/config_service_test.go (about)

     1  package github
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/github/hub/Godeps/_workspace/src/github.com/bmizerany/assert"
    10  	"github.com/github/hub/fixtures"
    11  )
    12  
    13  func TestConfigService_TomlLoad(t *testing.T) {
    14  	testConfig := fixtures.SetupTomlTestConfig()
    15  	defer testConfig.TearDown()
    16  
    17  	cc := &Config{}
    18  	cs := &configService{
    19  		Encoder: &tomlConfigEncoder{},
    20  		Decoder: &tomlConfigDecoder{},
    21  	}
    22  	err := cs.Load(testConfig.Path, cc)
    23  	assert.Equal(t, nil, err)
    24  
    25  	assert.Equal(t, 1, len(cc.Hosts))
    26  	host := cc.Hosts[0]
    27  	assert.Equal(t, "github.com", host.Host)
    28  	assert.Equal(t, "jingweno", host.User)
    29  	assert.Equal(t, "123", host.AccessToken)
    30  	assert.Equal(t, "http", host.Protocol)
    31  }
    32  
    33  func TestConfigService_YamlLoad(t *testing.T) {
    34  	testConfig := fixtures.SetupTestConfigs()
    35  	defer testConfig.TearDown()
    36  
    37  	cc := &Config{}
    38  	cs := &configService{
    39  		Encoder: &yamlConfigEncoder{},
    40  		Decoder: &yamlConfigDecoder{},
    41  	}
    42  	err := cs.Load(testConfig.Path, cc)
    43  	assert.Equal(t, nil, err)
    44  
    45  	assert.Equal(t, 1, len(cc.Hosts))
    46  	host := cc.Hosts[0]
    47  	assert.Equal(t, "github.com", host.Host)
    48  	assert.Equal(t, "jingweno", host.User)
    49  	assert.Equal(t, "123", host.AccessToken)
    50  	assert.Equal(t, "http", host.Protocol)
    51  }
    52  
    53  func TestConfigService_TomlSave(t *testing.T) {
    54  	file, _ := ioutil.TempFile("", "test-gh-config-")
    55  	defer os.RemoveAll(file.Name())
    56  
    57  	host := Host{
    58  		Host:        "github.com",
    59  		User:        "jingweno",
    60  		AccessToken: "123",
    61  		Protocol:    "https",
    62  	}
    63  	c := Config{Hosts: []Host{host}}
    64  
    65  	cs := &configService{
    66  		Encoder: &tomlConfigEncoder{},
    67  		Decoder: &tomlConfigDecoder{},
    68  	}
    69  	err := cs.Save(file.Name(), &c)
    70  	assert.Equal(t, nil, err)
    71  
    72  	b, _ := ioutil.ReadFile(file.Name())
    73  	content := `[[hosts]]
    74    host = "github.com"
    75    user = "jingweno"
    76    access_token = "123"
    77    protocol = "https"`
    78  	assert.Equal(t, content, strings.TrimSpace(string(b)))
    79  }
    80  
    81  func TestConfigService_YamlSave(t *testing.T) {
    82  	file, _ := ioutil.TempFile("", "test-gh-config-")
    83  	defer os.RemoveAll(file.Name())
    84  
    85  	host := Host{
    86  		Host:        "github.com",
    87  		User:        "jingweno",
    88  		AccessToken: "123",
    89  		Protocol:    "https",
    90  	}
    91  	c := Config{Hosts: []Host{host}}
    92  
    93  	cs := &configService{
    94  		Encoder: &yamlConfigEncoder{},
    95  		Decoder: &yamlConfigDecoder{},
    96  	}
    97  	err := cs.Save(file.Name(), &c)
    98  	assert.Equal(t, nil, err)
    99  
   100  	b, _ := ioutil.ReadFile(file.Name())
   101  	content := `github.com:
   102  - user: jingweno
   103    oauth_token: "123"
   104    protocol: https`
   105  	assert.Equal(t, content, strings.TrimSpace(string(b)))
   106  }