Categorygithub.com/go-corelibs/chdirs
modulepackage
1.1.2
Repository: https://github.com/go-corelibs/chdirs.git
Documentation: pkg.go.dev

# README

godoc codecov Go Report Card

chdirs - push/pop current working directory

chdirs is a package for managing the current working directory in a similar fashion to the unix shell pushd and popd functions.

Installation

> go get github.com/go-corelibs/chdirs@latest

Examples

Push, Pop

func main() {
    // need to do something in another directory
    // and ensure the program is returned to the
    // current working directory
    if err := chdirs.Push(".."); err != nil {
        panic(err)
    }
    defer chdirs.Pop()
    // do more stuff in the pushed directory...
    // the above defer will pop the path to the
    // original working directory when all is
    // done
}

MockBadWD, UnMockBadWD

// need to test important code when os.Getwd or filepath.Abs may fail,
// normally these tests are acceptably not written, however for command
// line programs where the current working directory could be anything,
// this testing is in fact quite necessary...

func Test(t *testing.T) {
    Convey("handling of os.Getwd/filepath.Abs errors", t, func() {
        err := MockBadWD()
        So(err, ShouldEqual, nil)
        /*
          ... test code handling os.Getwd and/or filepath.Abs errors ...
        */
        err = UnMockBadWD()
        So(err, ShouldEqual, nil)
    })
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

# Functions

MockBadWD is intended to be used during unit testing and not something generally useful.
Pop removes the last working directory from the stack and changes directory to it.
Push notes the current working directory and changes directory to the given path, use Pop to return to the previous working directory.
Stack returns the current Push stack.
UnMockBadWD restores the runtime to the original working directory when MockBadWD was called.