github.com/rogpeppe/juju@v0.0.0-20140613142852-6337964b789e/cmd/charm-admin/config_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"path"
    10  
    11  	"github.com/juju/cmd"
    12  	gc "launchpad.net/gocheck"
    13  
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type ConfigSuite struct {
    18  	testing.BaseSuite
    19  }
    20  
    21  var _ = gc.Suite(&ConfigSuite{})
    22  
    23  const testConfig = `
    24  mongo-url: localhost:23456
    25  foo: 1
    26  bar: false
    27  `
    28  
    29  func (s *ConfigSuite) SetUpSuite(c *gc.C) {
    30  	s.BaseSuite.SetUpSuite(c)
    31  }
    32  
    33  func (s *ConfigSuite) TearDownSuite(c *gc.C) {
    34  	s.BaseSuite.TearDownSuite(c)
    35  }
    36  
    37  type SomeConfigCommand struct {
    38  	ConfigCommand
    39  }
    40  
    41  func (c *SomeConfigCommand) Info() *cmd.Info {
    42  	return &cmd.Info{
    43  		Name:    "some-cmd",
    44  		Purpose: "something in particular that requires configuration",
    45  	}
    46  }
    47  
    48  func (c *SomeConfigCommand) Run(ctx *cmd.Context) error {
    49  	return c.ReadConfig(ctx)
    50  }
    51  
    52  func (s *ConfigSuite) TestReadConfig(c *gc.C) {
    53  	confDir := c.MkDir()
    54  	f, err := os.Create(path.Join(confDir, "charmd.conf"))
    55  	c.Assert(err, gc.IsNil)
    56  	cfgPath := f.Name()
    57  	{
    58  		defer f.Close()
    59  		fmt.Fprint(f, testConfig)
    60  	}
    61  
    62  	config := &SomeConfigCommand{}
    63  	args := []string{"--config", cfgPath}
    64  	err = testing.InitCommand(config, args)
    65  	c.Assert(err, gc.IsNil)
    66  	_, err = testing.RunCommand(c, config, args...)
    67  	c.Assert(err, gc.IsNil)
    68  
    69  	c.Assert(config.Config, gc.NotNil)
    70  	c.Assert(config.Config.MongoURL, gc.Equals, "localhost:23456")
    71  }