From 12126320a5f924708502ae989d00e00aaff0431e Mon Sep 17 00:00:00 2001 From: Jason Quesenberry Date: Fri, 17 May 2024 11:46:24 -0700 Subject: [PATCH] Adds filesystem and pause to demotools. Filesystem has a test, but pause does not because there is not resultant artifact to test. --- gov2/demotools/filesystem.go | 73 +++++++++++++++++++++++++++++++ gov2/demotools/filesystem_test.go | 55 +++++++++++++++++++++++ gov2/demotools/pause.go | 24 ++++++++++ 3 files changed, 152 insertions(+) create mode 100644 gov2/demotools/filesystem.go create mode 100644 gov2/demotools/filesystem_test.go create mode 100644 gov2/demotools/pause.go diff --git a/gov2/demotools/filesystem.go b/gov2/demotools/filesystem.go new file mode 100644 index 00000000000..096fc881203 --- /dev/null +++ b/gov2/demotools/filesystem.go @@ -0,0 +1,73 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package demotools provides a set of tools that help you write code examples. +// +// **FileSystem** +// +// The filesystem is used to abstract the os module and allow for +// variations to be swapped out, such as mocks for testing. +package demotools + +import ( + "io" + "os" +) + +// IFileSystem defines an interface that provides basic file i/o actions. +type IFileSystem interface { + Getwd() (string, error) + OpenFile(filename string) (io.ReadWriteCloser, error) + CloseFile(file io.ReadWriteCloser) +} + +// FileSystem is a struct to hold the injected object. +type FileSystem struct{} + +// NewStandardFileSystem uses the plain os module implementations. +func NewStandardFileSystem() *FileSystem { + return &FileSystem{} +} + +// Getwd returns the current working directory using the os module. +func (filesystem FileSystem) Getwd() (string, error) { + return os.Getwd() +} + +// OpenFile returns an io.ReadWriterCloser object based on the provided filename. +func (filesystem FileSystem) OpenFile(filename string) (io.ReadWriteCloser, error) { + file, err := os.Open(filename) + return file, err +} + +// CloseFile closes the provided file. +func (filesystem FileSystem) CloseFile(file io.ReadWriteCloser) { + defer file.Close() +} + +// MockFileSystem is a mock version of IFileSystem for testing. +type MockFileSystem struct { + mockfile io.ReadWriteCloser +} + +// NewMockFileSystem mocks the FileSystem and holds a singular io.ReadWriteCloser object to return. +func NewMockFileSystem(mockfile io.ReadWriteCloser) *MockFileSystem { + mockfilesystem := MockFileSystem{} + mockfilesystem.mockfile = mockfile + return &mockfilesystem +} + +// Getwd returns a basic mock directory string. +func (filesystem MockFileSystem) Getwd() (string, error) { + return "mock/dir", nil +} + +// OpenFile returns the io.ReadWriteCloser provided on object creation. +func (filesystem MockFileSystem) OpenFile(_ string) (io.ReadWriteCloser, error) { + return filesystem.mockfile, nil +} + +// CloseFile closes the io.ReadWriteCloser provided on object creation. +func (filesystem MockFileSystem) CloseFile(_ io.ReadWriteCloser) { + defer filesystem.mockfile.Close() +} diff --git a/gov2/demotools/filesystem_test.go b/gov2/demotools/filesystem_test.go new file mode 100644 index 00000000000..078bb1ce8c0 --- /dev/null +++ b/gov2/demotools/filesystem_test.go @@ -0,0 +1,55 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package demotools + +import ( + "os" + "testing" +) + +func TestNewStandardFileSystemGwd(t *testing.T) { + filesystem := NewStandardFileSystem() + testGwd, tErr := filesystem.Getwd() + realGwd, rErr := os.Getwd() + if testGwd != realGwd || tErr != nil || rErr != nil { + t.Errorf("NewStandardFileSystemGwd(): got %v, want %v. Errors(%v, %v)", testGwd, realGwd, tErr, rErr) + } +} + +func TestNewStandardFileSystemFileIO(t *testing.T) { + filesystem := NewStandardFileSystem() + filename := "test.txt" + os.Create(filename) + file, err := filesystem.OpenFile(filename) + if err != nil { + t.Errorf("NewStandardFileSystemFileInteraction(): error opening file: %v", err) + } + filesystem.CloseFile(file) + err = os.Remove(filename) + if err != nil { + t.Errorf("NewStandardFileSystemFileInteraction(): error removing file: %v", err) + } +} + +func TestNewMockFileSystem(t *testing.T) { + filename := "test.txt" + file, err := os.Create(filename) + if err != nil { + t.Errorf("NewMockFileSystem(): error creating file: %v", err) + } + filesystem := NewMockFileSystem(file) + mockGwd, gwdErr := filesystem.Getwd() + if mockGwd != "mock/dir" || gwdErr != nil { + t.Errorf("NewMockFileSystem(): %v is not correct or the error was: %v", mockGwd, gwdErr) + } + mockFile, fErr := filesystem.OpenFile("any string will do") + if fErr != nil { + t.Errorf("NewMockFileSystem(): error opening file: %v", fErr) + } + filesystem.CloseFile(mockFile) + err = os.Remove(filename) + if err != nil { + t.Errorf("NewMockFileSystem(): error removing file: %v", err) + } +} diff --git a/gov2/demotools/pause.go b/gov2/demotools/pause.go new file mode 100644 index 00000000000..2514a57daed --- /dev/null +++ b/gov2/demotools/pause.go @@ -0,0 +1,24 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Package demotools provides a set of tools that help you write code examples. +// +// **Pausable** +// +// The pausable interface creates an easy to mock pausing object for testing. +package demotools + +import "time" + +// IPausable defines the interface for pausable objects. +type IPausable interface { + Pause(secs int) +} + +// Pauser holds the pausable object. +type Pauser struct{} + +// Pause waits for the specified number of seconds. +func (pausable Pauser) Pause(secs int) { + time.Sleep(time.Duration(secs) * time.Second) +}