Skip to content

Commit

Permalink
Merge pull request #19 from seborama/CassettePath
Browse files Browse the repository at this point in the history
[CassettePath] Added support to provide custom location for fixtures
  • Loading branch information
seborama authored Jul 22, 2016
2 parents 87502b9 + 3ae9081 commit 372c853
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
36 changes: 23 additions & 13 deletions cassette.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -184,8 +185,8 @@ type Stats struct {

// cassette contains a set of tracks.
type cassette struct {
Name string
Tracks []track
Name, Path string
Tracks []track

// stats is unexported since it doesn't need serialising
stats Stats
Expand Down Expand Up @@ -225,7 +226,7 @@ func (k7 *cassette) save() error {
}

// write cassette to file
filename := cassetteNameToFilename(k7.Name)
filename := cassetteNameToFilename(k7.Name, k7.Path)
path := filepath.Dir(filename)
if err := os.MkdirAll(path, 0750); err != nil {
return err
Expand Down Expand Up @@ -265,8 +266,8 @@ func (k7 *cassette) numberOfTracks() int {
}

// DeleteCassette removes the cassette file from disk.
func DeleteCassette(cassetteName string) error {
filename := cassetteNameToFilename(cassetteName)
func DeleteCassette(cassetteName, cassettePath string) error {
filename := cassetteNameToFilename(cassetteName, cassettePath)

err := os.Remove(filename)
if os.IsNotExist(err) {
Expand All @@ -278,18 +279,27 @@ func DeleteCassette(cassetteName string) error {
}

// CassetteExistsAndValid verifies a cassette file exists and is seemingly valid.
func CassetteExistsAndValid(cassetteName string) bool {
_, err := readCassetteFromFile(cassetteName)
func CassetteExistsAndValid(cassetteName, cassettePath string) bool {
_, err := readCassetteFromFile(cassetteName, cassettePath)
return err == nil
}

// cassetteNameToFilename returns the filename associated to the cassette.
func cassetteNameToFilename(cassetteName string) string {
func cassetteNameToFilename(cassetteName, cassettePath string) string {
if cassetteName == "" {
return ""
}

return "./govcr-fixtures/" + cassetteName + ".cassette"
if cassettePath == "" {
cassettePath = defaultCassettePath
}

fpath, err := filepath.Abs(filepath.Join(cassettePath, cassetteName+".cassette"))
if err != nil {
log.Fatal(err)
}

return fpath
}

// transformInterfacesInJSON looks for known properties in the JSON that are defined as interface{}
Expand All @@ -311,8 +321,8 @@ func transformInterfacesInJSON(jsonString []byte) ([]byte, error) {
return []byte(regex.ReplaceAllString(string(jsonString), `$1"$2",`)), nil
}

func loadCassette(cassetteName string) (*cassette, error) {
k7, err := readCassetteFromFile(cassetteName)
func loadCassette(cassetteName, cassettePath string) (*cassette, error) {
k7, err := readCassetteFromFile(cassetteName, cassettePath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
Expand All @@ -329,8 +339,8 @@ func loadCassette(cassetteName string) (*cassette, error) {
}

// readCassetteFromFile reads the cassette file, if present.
func readCassetteFromFile(cassetteName string) (*cassette, error) {
filename := cassetteNameToFilename(cassetteName)
func readCassetteFromFile(cassetteName, cassettePath string) (*cassette, error) {
filename := cassetteNameToFilename(cassetteName, cassettePath)

// retrieve cassette from file
data, err := ioutil.ReadFile(filename)
Expand Down
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "github.com/seborama/govcr"
func runExample(name, cassetteName string, f func()) {
fmt.Println("Running " + name + "...")
fmt.Println("1st run =======================================================")
govcr.DeleteCassette(cassetteName)
govcr.DeleteCassette(cassetteName, "")
f()
fmt.Println("2nd run =======================================================")
f()
Expand Down
8 changes: 6 additions & 2 deletions govcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ func (vcr *VCRControlPanel) Stats() Stats {
return vcrT.Cassette.Stats()
}

const defaultCassettePath = "./govcr-fixtures/"

// VCRConfig holds a set of options for the VCR.
// TODO: update README.md with details of DisableRecording and FilterFunc's
type VCRConfig struct {
Client *http.Client
ExcludeHeaderFunc ExcludeHeaderFunc
RequestBodyFilterFunc BodyFilterFunc
DisableRecording bool
Logging bool
CassettePath string
}

// PCB stands for Printed Circuit Board. It is a structure that holds some
Expand All @@ -39,6 +41,7 @@ type pcb struct {
RequestBodyFilterFunc BodyFilterFunc
Logger *log.Logger
DisableRecording bool
CassettePath string
}

const trackNotFound = -1
Expand Down Expand Up @@ -135,7 +138,7 @@ func NewVCR(cassetteName string, vcrConfig *VCRConfig) *VCRControlPanel {
}

// load cassette
cassette, err := loadCassette(cassetteName)
cassette, err := loadCassette(cassetteName, vcrConfig.CassettePath)
if err != nil {
logger.Fatal(err)
}
Expand All @@ -148,6 +151,7 @@ func NewVCR(cassetteName string, vcrConfig *VCRConfig) *VCRControlPanel {
ExcludeHeaderFunc: vcrConfig.ExcludeHeaderFunc,
RequestBodyFilterFunc: vcrConfig.RequestBodyFilterFunc,
Logger: logger,
CassettePath: vcrConfig.CassettePath,
}

// create VCR's HTTP client
Expand Down
2 changes: 1 addition & 1 deletion govcr_example1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func runTestEx1() {
// http.Client suffices.
func Example_number1SimpleVCR() {
// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example1CassetteName)
govcr.DeleteCassette(example1CassetteName, "")

// 1st run of the test - will use live HTTP calls
runTestEx1()
Expand Down
2 changes: 1 addition & 1 deletion govcr_example2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Example_number2CustomClientVCR1() {
}

// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example2CassetteName)
govcr.DeleteCassette(example2CassetteName, "")

// 1st run of the test - will use live HTTP calls
runTestEx2(app)
Expand Down
2 changes: 1 addition & 1 deletion govcr_example3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func runTestEx4() {
// http.Client suffices.
func Example_number3HeaderExclusionVCR() {
// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example3CassetteName)
govcr.DeleteCassette(example3CassetteName, "")

// 1st run of the test - will use live HTTP calls
runTestEx4()
Expand Down
6 changes: 3 additions & 3 deletions govcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPlaybackOrder(t *testing.T) {

fmt.Println("Phase 1 ================================================")

if err := govcr.DeleteCassette(cassetteName); err != nil {
if err := govcr.DeleteCassette(cassetteName, ""); err != nil {
t.Fatalf("err from govcr.DeleteCassette(): Expected nil, got %s", err)
}

Expand All @@ -43,7 +43,7 @@ func TestPlaybackOrder(t *testing.T) {
// check outcome of the request
checkResponseForTestPlaybackOrder(t, cassetteName, vcr, resp, i)

if !govcr.CassetteExistsAndValid(cassetteName) {
if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
}

Expand All @@ -64,7 +64,7 @@ func TestPlaybackOrder(t *testing.T) {
// check outcome of the request
checkResponseForTestPlaybackOrder(t, cassetteName, vcr, resp, i)

if !govcr.CassetteExistsAndValid(cassetteName) {
if !govcr.CassetteExistsAndValid(cassetteName, "") {
t.Fatalf("CassetteExists: expected true, got false")
}

Expand Down

0 comments on commit 372c853

Please sign in to comment.