Closed harperreed closed 1 month ago
Certainly! I'll implement the test suite for the progress.go
file as requested. Let's break this down into steps and create the necessary code changes.
The user has requested to create a comprehensive test suite for the progress.go
file. This involves creating a new file progress_test.go
and implementing test cases for the Progress
struct and its methods (Increment()
and GetProgress()
), as well as the displayProgress()
function. We'll need to ensure thread safety, mock time.Sleep, and capture stdout for testing.
Let's create the test suite step by step.
progress_test.go
file:progress_test.go
---
+++
@@ -1 +1,98 @@
+package main
+import (
+ "bytes"
+ "io"
+ "os"
+ "sync"
+ "testing"
+ "time"
+)
+
+func TestProgressIncrement(t *testing.T) {
+ p := &Progress{totalFiles: 10}
+
+ p.Increment()
+ if p.processedFiles != 1 {
+ t.Errorf("Expected processedFiles to be 1, got %d", p.processedFiles)
+ }
+
+ p.Increment()
+ if p.processedFiles != 2 {
+ t.Errorf("Expected processedFiles to be 2, got %d", p.processedFiles)
+ }
+}
+
+func TestProgressGetProgress(t *testing.T) {
+ p := &Progress{totalFiles: 10, processedFiles: 5}
+
+ processed, total := p.GetProgress()
+ if processed != 5 || total != 10 {
+ t.Errorf("Expected (5, 10), got (%d, %d)", processed, total)
+ }
+}
+
+func TestProgressConcurrency(t *testing.T) {
+ p := &Progress{totalFiles: 1000}
+ var wg sync.WaitGroup
+
+ for i := 0; i < 1000; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ p.Increment()
+ }()
+ }
+
+ wg.Wait()
+
+ processed, total := p.GetProgress()
+ if processed != 1000 || total != 1000 {
+ t.Errorf("Expected (1000, 1000), got (%d, %d)", processed, total)
+ }
+}
+
+func TestDisplayProgress(t *testing.T) {
+ // Redirect stdout to capture output
+ old := os.Stdout
+ r, w, _ := os.Pipe()
+ os.Stdout = w
+
+ p := &Progress{totalFiles: 10, processedFiles: 0}
+
+ // Mock time.Sleep
+ oldSleep := time.Sleep
+ time.Sleep = func(d time.Duration) {}
+ defer func() { time.Sleep = oldSleep }()
+
+ done := make(chan bool)
+ go func() {
+ displayProgress(p)
+ done <- true
+ }()
+
+ // Simulate progress
+ for i := 0; i < 10; i++ {
+ p.Increment()
+ if i == 9 {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+
+ // Wait for displayProgress to finish
+ <-done
+
+ // Restore stdout
+ w.Close()
+ os.Stdout = old
+
+ var buf bytes.Buffer
+ io.Copy(&buf, r)
+ output := buf.String()
+
+ expectedOutput := "\rProcessed 9/10 images"
+ if output != expectedOutput {
+ t.Errorf("Expected output '%s', got '%s'", expectedOutput, output)
+ }
+}
Now, let's update the README.md file to mention the new test suite:
README.md
---
+++
@@ -3,8 +3,17 @@
- **hash.go**: Contains functions to compute file and perceptual hashes for images.
- **main.go**: Entry point for the application, manages scanning, hashing, finding similar images, and generating the report.
- **progress.go**: Handles progress display in the terminal.
+- **progress_test.go**: Contains the test suite for progress.go, ensuring correct functionality of the Progress struct and its methods.
- **report.go**: Contains logic to generate an HTML report from the found image groups.
- **scanner.go**: Recursively scans the directory for images.
- **similarity.go**: Implements algorithms to compare and group similar images.
+### Running Tests
+
+To run the test suite for progress.go, use the following command:
+
+```sh
+go test -v ./...
+```
+
Thanks for exploring Image Dupes Finder! 🎉 If you encounter any issues or have suggestions, feel free to open an issue or contribute to the project. Happy coding!
These changes implement a comprehensive test suite for the progress.go
file, addressing all the requirements specified in the user's request. Let's break down the implementation:
We created a new file progress_test.go
with the following test cases:
TestProgressIncrement
: Tests the Increment()
method.TestProgressGetProgress
: Tests the GetProgress()
method.TestProgressConcurrency
: Checks thread safety with concurrent increments.TestDisplayProgress
: Tests the displayProgress()
function, including mocking time.Sleep
and capturing stdout.The tests cover all the requested scenarios:
processedFiles
count increases correctly.GetProgress()
returns correct values.Increment()
.time.Sleep
to avoid long test durations.processed >= total
.We updated the README.md file to mention the new test suite and provide instructions on how to run the tests.
These changes provide a robust test suite for the progress.go
file, ensuring its functionality and thread safety.
Continue the conversation with Sweep here: https://app.sweep.dev/c/1f362362-c71c-4867-9267-012ee83310f6.
:book: For more information on how to use Sweep, please read our documentation.
Add test suite for progress.go
Description:
Create a comprehensive test suite for the
progress.go
file to ensure the correct functionality of theProgress
struct and its methods.Tasks:
progress_test.go
in the root directoryProgress
struct and its methods:Increment()
GetProgress()
displayProgress()
functionTest:
Increment()
:processedFiles
count increases correctlyGetProgress()
:processedFiles
andtotalFiles
valuesIncrement()
displayProgress()
:processed >= total
Additional Tasks:
README.md
to mention the new test suite forprogress.go