From d5affaa34738ffb39ca43c19fc400062197c2842 Mon Sep 17 00:00:00 2001 From: Tristan Rice Date: Thu, 22 Nov 2018 13:15:01 -0800 Subject: [PATCH] fixed history for JS builds --- pry/io_default.go | 16 +++++++------- pry/io_default_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++ pry/io_js.go | 16 +++++++------- pry/pry_test.go | 38 --------------------------------- 4 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 pry/io_default_test.go diff --git a/pry/io_default.go b/pry/io_default.go index 55dbb0a..0170fba 100644 --- a/pry/io_default.go +++ b/pry/io_default.go @@ -16,15 +16,15 @@ var readFile = ioutil.ReadFile var historyFile = ".go-pry_history" -type IOHistory struct { +type ioHistory struct { FileName string FilePath string Records []string } -// NewHistory constructs IOHistory instance -func NewHistory() (*IOHistory, error) { - h := IOHistory{} +// NewHistory constructs ioHistory instance +func NewHistory() (*ioHistory, error) { + h := ioHistory{} h.FileName = historyFile dir, err := homedir.Dir() @@ -38,7 +38,7 @@ func NewHistory() (*IOHistory, error) { } // Load unmarshal history file into history's records -func (h *IOHistory) Load() error { +func (h *ioHistory) Load() error { body, err := ioutil.ReadFile(h.FilePath) if err != nil { return errors.Wrapf(err, "History file not found") @@ -53,7 +53,7 @@ func (h *IOHistory) Load() error { } // Save saves marshaled history's records into file -func (h IOHistory) Save() error { +func (h ioHistory) Save() error { body, err := json.Marshal(h.Records) if err != nil { return errors.Wrapf(err, "error marshaling history") @@ -66,9 +66,9 @@ func (h IOHistory) Save() error { } // Len returns amount of records in history -func (h IOHistory) Len() int { return len(h.Records) } +func (h ioHistory) Len() int { return len(h.Records) } // Add appends record into history's records -func (h *IOHistory) Add(record string) { +func (h *ioHistory) Add(record string) { h.Records = append(h.Records, record) } diff --git a/pry/io_default_test.go b/pry/io_default_test.go new file mode 100644 index 0000000..fe96e57 --- /dev/null +++ b/pry/io_default_test.go @@ -0,0 +1,48 @@ +// +build !js + +package pry + +import ( + "fmt" + "math/rand" + "os" + "path/filepath" + "reflect" + "testing" + "time" +) + +func TestHistory(t *testing.T) { + t.Parallel() + + rand.Seed(time.Now().UnixNano()) + + history := &ioHistory{} + history.FileName = ".go-pry_history_test" + history.FilePath = filepath.Join(os.TempDir(), history.FileName) + + expected := []string{ + "test", + fmt.Sprintf("rand: %d", rand.Int63()), + } + history.Add(expected[0]) + history.Add(expected[1]) + + if err := history.Save(); err != nil { + t.Error("Failed to save history") + } + + if err := history.Load(); err != nil { + t.Error("Failed to load history") + } + + if !reflect.DeepEqual(expected, history.Records) { + t.Errorf("history.Load() = %+v; expected %+v", history.Records, expected) + } + + // delete test history file + err := os.Remove(history.FilePath) + if err != nil { + t.Error(err) + } +} diff --git a/pry/io_js.go b/pry/io_js.go index 36e54e9..0e7fe08 100644 --- a/pry/io_js.go +++ b/pry/io_js.go @@ -31,21 +31,21 @@ func readFile(path string) ([]byte, error) { return ioutil.ReadAll(r) } -type BrowserHistory struct { +type browserHistory struct { Records []string } -// NewHistory constructs BrowserHistory instance -func NewHistory() (*BrowserHistory, error) { +// NewHistory constructs browserHistory instance +func NewHistory() (*browserHistory, error) { // FIXME: // when localStorage is full, can be return an error - return &BrowserHistory{}, nil + return &browserHistory{}, nil } // Load unmarshal localStorage data into history's records -func (bh *BrowserHistory) Load() error { +func (bh *browserHistory) Load() error { hist := js.Global().Get("localStorage").Get("history") if hist.Type() == js.TypeUndefined { return nil // nothing to unmarashal @@ -60,7 +60,7 @@ func (bh *BrowserHistory) Load() error { } // Save saves marshaled history's records into localStorage -func (bh BrowserHistory) Save() error { +func (bh browserHistory) Save() error { bytes, err := json.Marshal(bh.Records) if err != nil { return err @@ -71,9 +71,9 @@ func (bh BrowserHistory) Save() error { } // Len returns amount of records in history -func (bh BrowserHistory) Len() int { return len(bh.Records) } +func (bh browserHistory) Len() int { return len(bh.Records) } // Add appends record into history's records -func (bh *BrowserHistory) Add(record string) { +func (bh *browserHistory) Add(record string) { bh.Records = append(bh.Records, record) } diff --git a/pry/pry_test.go b/pry/pry_test.go index 826756a..77983aa 100644 --- a/pry/pry_test.go +++ b/pry/pry_test.go @@ -1,13 +1,10 @@ package pry import ( - "fmt" "io" "io/ioutil" - "math/rand" "os" "path" - "path/filepath" "reflect" "testing" "time" @@ -17,41 +14,6 @@ import ( "github.com/pkg/errors" ) -func TestHistory(t *testing.T) { - t.Parallel() - - rand.Seed(time.Now().UnixNano()) - - history := &IOHistory{} - history.FileName = ".go-pry_history_test" - history.FilePath = filepath.Join(os.TempDir(), history.FileName) - - expected := []string{ - "test", - fmt.Sprintf("rand: %d", rand.Int63()), - } - history.Add(expected[0]) - history.Add(expected[1]) - - if err := history.Save(); err != nil { - t.Error("Failed to save history") - } - - if err := history.Load(); err != nil { - t.Error("Failed to load history") - } - - if !reflect.DeepEqual(expected, history.Records) { - t.Errorf("history.Load() = %+v; expected %+v", history.Records, expected) - } - - // delete test history file - err := os.Remove(history.FilePath) - if err != nil { - t.Error(err) - } -} - func TestCLIBasicStatement(t *testing.T) { t.Parallel()