Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: updates Package Structure, Add Interfaces, and Enhance Testing #29

Merged
merged 14 commits into from
Jun 28, 2024

Conversation

supitsdu
Copy link
Owner

@supitsdu supitsdu commented Jun 27, 2024

This pull request addresses a few key changes (See more #24) and significantly improves the code structure and testability of clipper.

Key Changes:

  • Package Renaming: The main package has been renamed from main to clipper for better clarity and consistency.
  • Interfaces:
    • Introduced ContentReader and ClipboardWriter interfaces to improve flexibility and allow for different content sources and clipboard implementations.
    • This change makes the code more modular and easier to test.
  • Options Handling:
    • Created a dedicated options package to centralize command-line argument parsing and configuration management.
    • This improves code organization and maintainability.
  • Dependency Injection: The Run function now accepts a ClipboardWriter as a parameter, enabling easier mocking in tests.
  • Test Suite:

Additional Changes:

  • Removed the old main.go and main_test.go files, as their functionality has been migrated to the new structure.

Benefits:

  • Improved code organization and readability.
  • Enhanced testability through the use of interfaces and dependency injection.
  • Increased flexibility for future extensions and integrations.

Resolves #24

How to Test:

  1. Checkout this branch.
  2. Run the tests using go test ./.... All tests should pass.
  3. Run the clipper tool with different options and input sources to verify correct behavior.

Screenshots/GIFs (optional):

  • You could include screenshots or GIFs demonstrating the updated test coverage or any visual changes in the output.

Checklist:

  • All unit tests pass.
  • The clipper tool functions as expected with various options and input sources.
  • The code is well-documented and adheres to the project's coding style.

Additional Notes:

This pull request is a major step towards a more modular, testable, and maintainable codebase for the clipper tool. It sets the foundation for future enhancements and features.

supitsdu added 8 commits June 26, 2024 11:14
Removed main_test.go as its functionality is now integrated into more specific test files.
Removed main.go file to migrate functionality to the more appropriately named cli/main.go.
Define a configuration struct for command line options to manage and parse command line arguments effectively.
Introduce interfaces for content reading and clipboard writing to enhance flexibility and testability.
Refactor command line argument handling into a dedicated CLI package for improved organization and clarity.
- Modify the Run function to accept a ClipboardWriter as a parameter instead of creating a DefaultClipboardWriter directly. This would enable easier mocking of the clipboard for testing.
This commit introduces a new `tests/clipper_test.go` file, which includes comprehensive unit tests for the clipper package. The tests cover various components including:
- FileContentReader: Reads content from a file.
- StdinContentReader: Reads content from standard input.
- DefaultClipboardWriter: Writes content to the clipboard.
- ParseContent: Parses content from multiple sources, including direct text, files, and invalid inputs.

Additionally, the tests use helper functions for creating temporary files and replacing stdin, improving readability and maintainability.
@supitsdu supitsdu added enhancement New feature requests or enhancements. testing Issues or PRs specifically related to Go testing frameworks or practices. refactoring Issues or PRs to improving code structure, without changing its external behavior under review Issues or PRs currently under evaluation and discussion. cleanup Minor changes to remove dead code, fix inconsistencies, etc. labels Jun 27, 2024
@supitsdu supitsdu self-assigned this Jun 27, 2024
cli/clipper/clipper.go Outdated Show resolved Hide resolved
cli/clipper/clipper.go Outdated Show resolved Hide resolved
cli/clipper/clipper.go Show resolved Hide resolved
cli/clipper/clipper.go Outdated Show resolved Hide resolved
cli/clipper/clipper.go Outdated Show resolved Hide resolved
cli/main.go Outdated Show resolved Hide resolved
tests/clipper_test.go Show resolved Hide resolved
tests/clipper_test.go Show resolved Hide resolved
tests/clipper_test.go Show resolved Hide resolved
tests/clipper_test.go Show resolved Hide resolved
supitsdu added 4 commits June 28, 2024 00:27
Skipping clipboard test in short mode. Helps avoid errors when on CI environments.
…and exiting

- Refactored the `Run` function to return a `string` (representing the success message or version information) and an `error`.
- This change allows the caller of `Run` to handle the output and errors gracefully, rather than relying on `Run` to print messages and exit directly.
- Updated the error handling to use `fmt.Errorf` with error wrapping (%w) for better context.
- This improvement enhances the flexibility and testability of the `clipper` tool.
… values

- Modified the `main` function to call `clipper.Run` and handle both the returned message and error.
- Prints the success message if no error occurred, or an error message if an error was encountered.
- Exits with a status code of 0 for success or 1 for errors.
- This change ensures that the `main` function gracefully handles the output and errors generated by the `Run` function.
- Introduced a new function `GetReaders` in `clipper.go` to handle the logic of creating `ContentReader` instances based on command-line arguments.
- This refactoring improves the readability and maintainability of the `Run` function by separating out a specific task.
- The `Run` function now calls `GetReaders` to obtain the necessary readers, making its logic more focused and concise.
@supitsdu supitsdu merged commit 3301698 into main Jun 28, 2024
1 check passed
@supitsdu supitsdu deleted the refact/update-overall-code-structure branch June 28, 2024 06:01
@supitsdu supitsdu added in-progress Issues or PRs currently being worked on. and removed under review Issues or PRs currently under evaluation and discussion. labels Jun 28, 2024

if msg != "" {
fmt.Printf("Clipper %s\n", msg)
os.Exit(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Minor changes to remove dead code, fix inconsistencies, etc. enhancement New feature requests or enhancements. in-progress Issues or PRs currently being worked on. refactoring Issues or PRs to improving code structure, without changing its external behavior testing Issues or PRs specifically related to Go testing frameworks or practices.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor Package Name for Clarity (from main to clipper or clipboardtool)
2 participants