File: aliasfile_test.go

package info (click to toggle)
golang-github-juju-cmd 3.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: makefile: 7
file content (58 lines) | stat: -rw-r--r-- 1,317 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.

package cmd_test

import (
	_ "fmt"
	"io/ioutil"
	"path/filepath"

	"github.com/juju/testing"
	gc "gopkg.in/check.v1"

	"github.com/juju/cmd/v3"
)

type ParseAliasFileSuite struct {
	testing.LoggingSuite
}

var _ = gc.Suite(&ParseAliasFileSuite{})

func (*ParseAliasFileSuite) TestMissing(c *gc.C) {
	dir := c.MkDir()
	filename := filepath.Join(dir, "missing")
	aliases := cmd.ParseAliasFile(filename)
	c.Assert(aliases, gc.NotNil)
	c.Assert(aliases, gc.HasLen, 0)
}

func (*ParseAliasFileSuite) TestParse(c *gc.C) {
	dir := c.MkDir()
	filename := filepath.Join(dir, "missing")
	content := `
# comments skipped, as are the blank lines, such as the line
# at the start of this file
   foo =  trailing-space    
repeat = first
flags = flags  --with   flag

# if the same alias name is used more than once, last one wins
repeat = second

# badly formated values are logged, but skipped
no equals sign
=
key = 
= value
`
	err := ioutil.WriteFile(filename, []byte(content), 0644)
	c.Assert(err, gc.IsNil)
	aliases := cmd.ParseAliasFile(filename)
	c.Assert(aliases, gc.DeepEquals, map[string][]string{
		"foo":    []string{"trailing-space"},
		"repeat": []string{"second"},
		"flags":  []string{"flags", "--with", "flag"},
	})
}