chdirs

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: Apache-2.0 Imports: 2 Imported by: 3

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockBadWD added in v1.1.0

func MockBadWD() (err error)

MockBadWD is intended to be used during unit testing and not something generally useful. MockBadWD changes to a new temp directory and then deletes the directory. This leaves the running code in a case where calling os.Getwd will throw and error, which in turn causes filepath.Abs to throw an error

Call UnMockBadWD to restore the working directory to the location the runtime was at when MockBadWD was called

func Pop

func Pop() (err error)

Pop removes the last working directory from the stack and changes directory to it

func Push

func Push(path string) (err error)

Push notes the current working directory and changes directory to the given path, use Pop to return to the previous working directory

func Stack

func Stack() (stack []string)

Stack returns the current Push stack

func UnMockBadWD added in v1.1.0

func UnMockBadWD() (err error)

UnMockBadWD restores the runtime to the original working directory when MockBadWD was called

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL