Merge lp:~themue/juju-core/016-juju-home into lp:~juju/juju-core/trunk

Proposed by Frank Mueller
Status: Merged
Approved by: William Reade
Approved revision: no longer in the source branch.
Merged at revision: 1013
Proposed branch: lp:~themue/juju-core/016-juju-home
Merge into: lp:~juju/juju-core/trunk
Diff against target: 130 lines (+121/-0)
2 files modified
environs/config/home.go (+56/-0)
environs/config/home_test.go (+65/-0)
To merge this branch: bzr merge lp:~themue/juju-core/016-juju-home
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+153742@code.launchpad.net

Description of the change

config: added functions to access juju home

This first CL introduces functions for the access to
$JUJU_HOME - if set - or $HOME/.juju as fallback. The
functions return the directory, allow to create a path
to a file/dir in it, set it for testing purposes and
restore it afterwards. A second CL will then migrate
all manual access to $HOME/.juju today to the usage
of the new API.

https://codereview.appspot.com/7774044/

To post a comment you must log in.
Revision history for this message
Dimiter Naydenov (dimitern) wrote :

LGTM with a trivial suggestion.

https://codereview.appspot.com/7774044/diff/1/environs/config/home_test.go
File environs/config/home_test.go (right):

https://codereview.appspot.com/7774044/diff/1/environs/config/home_test.go#newcode26
environs/config/home_test.go:26: config.RestoreJujuHome()
probably you don't need this in both setup and teardown

https://codereview.appspot.com/7774044/

Revision history for this message
William Reade (fwereade) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'environs/config/home.go'
--- environs/config/home.go 1970-01-01 00:00:00 +0000
+++ environs/config/home.go 2013-03-18 10:29:22 +0000
@@ -0,0 +1,56 @@
1package config
2
3import (
4 "os"
5 "path/filepath"
6 "sync"
7)
8
9// jujuHome stores the path to the juju configuration
10// folder defined by $JUJU_HOME or default ~/.juju.
11var (
12 jujuHomeMu sync.Mutex
13 jujuHome string
14)
15
16func init() {
17 RestoreJujuHome()
18}
19
20// JujuHome returns the current juju home.
21func JujuHome() string {
22 return jujuHome
23}
24
25// JujuHomePath returns the path to a file in the
26// current juju home.
27func JujuHomePath(names ...string) string {
28 all := append([]string{jujuHome}, names...)
29 return filepath.Join(all...)
30}
31
32// SetTestJujuHome allows to set the value of juju home for test
33// purposes. It returns the current juju home.
34func SetTestJujuHome(home string) string {
35 jujuHomeMu.Lock()
36 defer jujuHomeMu.Unlock()
37
38 current := jujuHome
39 jujuHome = home
40 return current
41}
42
43// RestoreJujuHome (re)initializes the juju home after it may
44// have been changed for testing purposes. It returns the
45// juju home.
46func RestoreJujuHome() string {
47 jujuHomeMu.Lock()
48 defer jujuHomeMu.Unlock()
49
50 jujuHome = os.Getenv("JUJU_HOME")
51 if jujuHome == "" {
52 home := os.Getenv("HOME")
53 jujuHome = filepath.Join(home, ".juju")
54 }
55 return jujuHome
56}
057
=== added file 'environs/config/home_test.go'
--- environs/config/home_test.go 1970-01-01 00:00:00 +0000
+++ environs/config/home_test.go 2013-03-18 10:29:22 +0000
@@ -0,0 +1,65 @@
1package config_test
2
3import (
4 . "launchpad.net/gocheck"
5 "os"
6 "path/filepath"
7
8 "launchpad.net/juju-core/environs/config"
9)
10
11type JujuHomeSuite struct {
12 origJujuHome string
13}
14
15var _ = Suite(&JujuHomeSuite{})
16
17func (s *JujuHomeSuite) SetUpSuite(c *C) {
18 s.origJujuHome = config.JujuHome()
19}
20
21func (s *JujuHomeSuite) TearDownSuite(c *C) {
22 config.RestoreJujuHome()
23}
24
25func (s *JujuHomeSuite) SetUpTest(c *C) {
26 config.RestoreJujuHome()
27}
28
29func (s *JujuHomeSuite) TearDownTest(c *C) {
30 err := os.Setenv("JUJU_HOME", s.origJujuHome)
31 c.Assert(err, IsNil)
32}
33
34func (s *JujuHomeSuite) TestStandardHome(c *C) {
35 home := os.Getenv("HOME")
36 err := os.Setenv("JUJU_HOME", "")
37 c.Assert(err, IsNil)
38 jujuHome := config.RestoreJujuHome()
39 newJujuHome := filepath.Join(home, ".juju")
40 c.Assert(jujuHome, Equals, newJujuHome)
41 jujuHome = config.JujuHome()
42 c.Assert(jujuHome, Equals, newJujuHome)
43 testJujuHome := c.MkDir()
44 err = os.Setenv("JUJU_HOME", testJujuHome)
45 jujuHome = config.RestoreJujuHome()
46 c.Assert(jujuHome, Equals, testJujuHome)
47 jujuHome = config.JujuHome()
48 c.Assert(jujuHome, Equals, testJujuHome)
49}
50
51func (s *JujuHomeSuite) TestHomePath(c *C) {
52 envPath := config.JujuHomePath("environments.yaml")
53 c.Assert(envPath, Equals, filepath.Join(s.origJujuHome, "environments.yaml"))
54}
55
56func (s *JujuHomeSuite) TestTestHome(c *C) {
57 jujuHome := config.JujuHome()
58 testJujuHome := c.MkDir()
59 origJujuHome := config.SetTestJujuHome(testJujuHome)
60 c.Assert(jujuHome, Equals, origJujuHome)
61 jujuHome = config.JujuHome()
62 c.Assert(jujuHome, Equals, testJujuHome)
63 jujuHome = config.RestoreJujuHome()
64 c.Assert(jujuHome, Equals, origJujuHome)
65}

Subscribers

People subscribed via source and target branches