diff options
| author | Craig Jennings <c@cjennings.net> | 2025-11-04 07:56:31 -0600 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2025-11-04 07:56:31 -0600 |
| commit | 6cf0aac96ae0abb455ee8525e0ede9a63f5974f3 (patch) | |
| tree | 82212136eae5ba429223739dff3b6683e01c3331 /wttrin.el | |
| parent | 4d8973dcc5c3c3d043011e67c8d4e9d581df6a43 (diff) | |
| download | emacs-wttrin-6cf0aac96ae0abb455ee8525e0ede9a63f5974f3.tar.gz emacs-wttrin-6cf0aac96ae0abb455ee8525e0ede9a63f5974f3.zip | |
Add comprehensive ERT test suite and fix critical bugs
Session 1: Testing infrastructure and initial test coverage
Bug fixes in wttrin.el:
- Fix wttrin-additional-url-params to handle nil unit system
- Remove incorrect callback parameter to url-retrieve-synchronously
- Add nil buffer check for network failures
- Strip HTTP headers before decoding response
- Kill buffer after fetch to prevent memory leaks
- Fix double concatenation of URL params in cache function
- Add proper URL encoding via new wttrin--build-url function
Refactoring:
- Extract wttrin--build-url as pure, testable function
- Separate URL building logic from network I/O
Test infrastructure (33 tests, 100% passing):
- tests/testutil-wttrin.el: Shared test utilities
- tests/test-wttrin-additional-url-params.el: 7 tests
- tests/test-wttrin--make-cache-key.el: 9 tests
- tests/test-wttrin--build-url.el: 10 tests
- tests/test-wttrin--cleanup-cache-if-needed.el: 7 tests
Documentation:
- docs/testing-plan.org: Comprehensive testing roadmap
- docs/bugs.org: Bug analysis from code review
- docs/NOTES.org: Session tracking and guidelines
- docs/session-1-summary.org: Detailed session summary
Next session: Cache workflow tests, parsing logic extraction, integration tests
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'wttrin.el')
| -rw-r--r-- | wttrin.el | 42 |
1 files changed, 31 insertions, 11 deletions
@@ -102,17 +102,38 @@ units (default)." (defun wttrin-additional-url-params () "Concatenates extra information into the URL." - (concat "?" wttrin-unit-system)) + (if wttrin-unit-system + (concat "?" wttrin-unit-system) + "?")) + +(defun wttrin--build-url (query) + "Build wttr.in URL for QUERY with configured parameters. +This is a pure function with no side effects, suitable for testing." + (when (null query) + (error "Query cannot be nil")) + (concat "https://wttr.in/" + (url-hexify-string query) + (wttrin-additional-url-params) + "A")) (defun wttrin-fetch-raw-string (query) - "Get the weather information based on your QUERY." - (let ((url-request-extra-headers (list wttrin-default-languages)) - (url-user-agent "curl")) - (with-current-buffer - (url-retrieve-synchronously - (concat "https://wttr.in/" query (wttrin-additional-url-params) "A") - (lambda () (switch-to-buffer (current-buffer)))) - (decode-coding-string (buffer-string) 'utf-8)))) + "Get the weather information based on your QUERY. +Returns the weather data as a string, or signals an error on failure." + (let* ((url (wttrin--build-url query)) + (url-request-extra-headers (list wttrin-default-languages)) + (url-user-agent "curl") + (buf (url-retrieve-synchronously url t t))) + (unless buf + (error "wttrin: Network failure - could not retrieve %S" query)) + (unwind-protect + (with-current-buffer buf + ;; Skip HTTP headers + (goto-char (point-min)) + (re-search-forward "\r?\n\r?\n" nil t) + (decode-coding-string + (buffer-substring-no-properties (point) (point-max)) + 'utf-8)) + (kill-buffer buf)))) (defun wttrin-exit () "Exit the wttrin buffer." @@ -197,8 +218,7 @@ Returns the weather data string or nil on error." (not wttrin--force-refresh)) data (condition-case err - (let ((fresh-data (wttrin-fetch-raw-string - (concat location (wttrin-additional-url-params))))) + (let ((fresh-data (wttrin-fetch-raw-string location))) (when fresh-data (wttrin--cleanup-cache-if-needed) (puthash cache-key (cons now fresh-data) wttrin--cache)) |
