-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds filesystem and pause to demotools. Filesystem has a test, but pa…
…use does not because there is not resultant artifact to test.
- Loading branch information
1 parent
e7a87c4
commit 1212632
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |