Skip to content

Commit

Permalink
Merge pull request kidd#192 from telotortium/pr-179
Browse files Browse the repository at this point in the history
Adopt kidd#179, merging current master into this and fixing tests.

- Move to beginning of line everywhere before running `org-element-headline-parser`.
- Prevent archiving in a cycle.

Fix kidd#172
  • Loading branch information
telotortium authored Mar 24, 2022
2 parents 47cb514 + 368e4d2 commit 554c48f
Show file tree
Hide file tree
Showing 7 changed files with 602 additions and 22 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,10 @@ jobs:

- name: Test
run: make test

- name: SSH into container on failure
if: ${{ failure() }}
uses: lhotari/action-upterm@v1
with:
## limits ssh access and adds the ssh public key for the user which triggered the workflow
limit-access-to-actor: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.load-path.el
.cask
build.log

flycheck_*.el
3 changes: 2 additions & 1 deletion Cask
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

(development
(depends-on "el-mock")
(depends-on "ert-runner"))
(depends-on "ert-runner")
(depends-on "load-relative"))
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
THIS_MAKEFILE_DIR = $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
EMACS ?= emacs
SRC=org-gcal.el org-generic-id.el
TEST=test/org-gcal-test.el test/org-generic-id-test.el
Expand All @@ -24,4 +25,5 @@ compile: $(SRC) elpa
! ( grep -E -e ':(Warning|Error):' $(BUILD_LOG) )

test: $(SRC) $(TEST) elpa
$(CASK) exec ert-runner -L . -L test/
$(CASK) exec ert-runner -L $(THIS_MAKEFILE_DIR) \
$(foreach test,$(TEST),$(addprefix $(THIS_MAKEFILE_DIR)/,$(test)))
48 changes: 29 additions & 19 deletions org-gcal.el
Original file line number Diff line number Diff line change
Expand Up @@ -1486,25 +1486,35 @@ delete calendar info from events on calendars you no longer have access to."
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-heading-regexp nil t)
(condition-case nil
(goto-char (cdr (org-gcal--timestamp-successor)))
(error (error "Org-gcal error: Couldn't parse %s"
(buffer-file-name))))
(let ((elem (org-element-headline-parser (point-max) t))
(tobj (cadr (org-element-timestamp-parser))))
(when (>
(time-to-seconds (time-subtract (current-time) (days-to-time org-gcal-up-days)))
(time-to-seconds (encode-time 0 (if (plist-get tobj :minute-end)
(plist-get tobj :minute-end) 0)
(if (plist-get tobj :hour-end)
(plist-get tobj :hour-end) 24)
(plist-get tobj :day-end)
(plist-get tobj :month-end)
(plist-get tobj :year-end))))
(org-gcal--notify "Archived event." (org-element-property :title elem))
(let ((kill-ring kill-ring)
(select-enable-clipboard nil))
(org-archive-subtree)))))
(let ((properties (org-entry-properties)))
; Check if headline is managed by `org-gcal', and hasn't been archived
; yet. Only in that case, potentially archive.
(when (and (assoc "ORG-GCAL-MANAGED" properties)
(not (assoc "ARCHIVE_TIME" properties)))

; Go to beginning of line to parse the headline
(beginning-of-line)
(let ((elem (org-element-headline-parser (point-max) t)))

; Go to next timestamp to parse it
(condition-case nil
(goto-char (cdr (org-gcal--timestamp-successor)))
(error (error "Org-gcal error: Couldn't parse %s"
(buffer-file-name))))
(let ((tobj (cadr (org-element-timestamp-parser))))
(when (>
(time-to-seconds (time-subtract (current-time) (days-to-time org-gcal-up-days)))
(time-to-seconds (encode-time 0 (if (plist-get tobj :minute-end)
(plist-get tobj :minute-end) 0)
(if (plist-get tobj :hour-end)
(plist-get tobj :hour-end) 24)
(plist-get tobj :day-end)
(plist-get tobj :month-end)
(plist-get tobj :year-end))))
(org-gcal--notify "Archived event." (org-element-property :title elem))
(let ((kill-ring kill-ring)
(select-enable-clipboard nil))
(org-archive-subtree))))))))
(save-buffer)))

(defun org-gcal--save-sexp (data file)
Expand Down
51 changes: 50 additions & 1 deletion test/org-gcal-test.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;; org-gcal-test.el --- Tests for org-gcal.el -*- lexical-binding: t -*-

;; Copyright (C) 2019 Robert Irelan
;; Package-Requires: ((org-gcal) (el-mock) (emacs "26"))
;; Package-Requires: ((org-gcal) (el-mock) (emacs "26") (load-relative "1.3"))

;; Author: Robert Irelan <[email protected]>

Expand Down Expand Up @@ -29,6 +29,9 @@
(require 'org-gcal)
(require 'cl-lib)
(require 'el-mock)
(require 'load-relative)
(unless (featurep 'org-test)
(load-relative "org-test"))

(defconst org-gcal-test-calendar-id "[email protected]")

Expand Down Expand Up @@ -1224,6 +1227,52 @@ Second paragraph
;; "2021-03-04T03:30:00+0800"))


(ert-deftest org-gcal-test--headline-archive-old-event ()
"Check that `org-gcal--archive-old-event' parses headlines correctly.
Regression test for https://github.com/kidd/org-gcal.el/issues/172 .
Also tests that the `org-gcal--archive-old-event' function does
not loop over and over, archiving the same entry because it is
under another heading in the same file."
(let ((org-archive-location "::* Archived") ; Make the archive this same buffer
(test-time "2022-01-30 Sun 01:23")
(buf "\
#+CATEGORY: Test
* Event Title
:PROPERTIES:
:org-gcal-managed: something
:END:
<2021-01-01 Fri 12:34-14:35>
"))
(org-test-with-temp-text-in-file
buf
(org-test-at-time (format "<%s>" test-time)
;; Ensure property drawer is not indented
(setq-local org-adapt-indentation nil)
(let* ((target-buf (format "\
#+CATEGORY: Test
* Archived
** Event Title
:PROPERTIES:
:org-gcal-managed: something
:ARCHIVE_TIME: %s
:ARCHIVE_FILE: %s
:ARCHIVE_CATEGORY: Test
:END:
<2021-01-01 Fri 12:34-14:35>
"
test-time
; The variable `file' is the current file
; name under the macro
; `org-test-with-temp-text-in-file'
file)))
(org-gcal--archive-old-event)
(let ((bufstr
(buffer-substring-no-properties (point-min) (point-max))))
(should (string-equal bufstr target-buf))))))))

;;; TODO: Figure out mocking for POST/PATCH followed by GET
;;; - ‘mock‘ might work for this - the argument list must be specified up
Expand Down
Loading

0 comments on commit 554c48f

Please sign in to comment.