Skip to content

Commit

Permalink
dirs: rewrite and test StripRootDir
Browse files Browse the repository at this point in the history
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
  • Loading branch information
zyga committed Aug 25, 2016
1 parent 52d785e commit 18eb8c3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
15 changes: 12 additions & 3 deletions dirs/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package dirs

import (
"fmt"
"os"
"path/filepath"
"strings"
)

// the various file paths
Expand Down Expand Up @@ -75,10 +77,17 @@ func init() {

// StripRootDir strips the custom global root directory from the specified argument.
func StripRootDir(dir string) string {
if GlobalRootDir == "/" {
return dir
if !filepath.IsAbs(dir) {
panic(fmt.Sprintf("supplied path is not absolute %q", dir))
}
return dir[len(GlobalRootDir):]
if !strings.HasPrefix(dir, GlobalRootDir) {
panic(fmt.Sprintf("supplied path is not related to global root %q", dir))
}
result, err := filepath.Rel(GlobalRootDir, dir)
if err != nil {
panic(err)
}
return "/" + result
}

// SetRootDir allows settings a new global root directory, this is useful
Expand Down
49 changes: 49 additions & 0 deletions dirs/dirs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package dirs_test

import (
"testing"

. "gopkg.in/check.v1"

"github.com/snapcore/snapd/dirs"
)

// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }

var _ = Suite(&DirsTestSuite{})

type DirsTestSuite struct{}

func (s *DirsTestSuite) TestStripRootDir(c *C) {
// strip does nothing if the default (empty) root directory is used
c.Check(dirs.StripRootDir("/foo/bar"), Equals, "/foo/bar")
// strip only works on absolute paths
c.Check(func() { dirs.StripRootDir("relative") }, Panics, `supplied path is not absolute "relative"`)
// with an alternate root
dirs.SetRootDir("/alt/")
defer dirs.SetRootDir("")
// strip behaves as expected, returning absolute paths without the prefix
c.Check(dirs.StripRootDir("/alt/foo/bar"), Equals, "/foo/bar")
// strip only works on paths that begin with the global root directory
c.Check(func() { dirs.StripRootDir("/other/foo/bar") }, Panics, `supplied path is not related to global root "/other/foo/bar"`)
}

0 comments on commit 18eb8c3

Please sign in to comment.