h-tests: add basic infrastructure to create dummy code-root setup

So far, I was hoping I could check-in some dummy code-root setups
directly to the git repository as fixtures. Sadly, it's almost
impossible to check-in git repos in a git repo. The only option do to
so would be to setup git subrepo. Needless to say, I *really* don't
want to go down that path.

Instead, I created some infrastructure to create a temp directory in
which we generate the test code-roots on the fly.
This commit is contained in:
Félix Baylac-Jacqué 2022-04-02 11:56:24 +02:00
parent 02109cec18
commit 0abe2e0956
No known key found for this signature in database
GPG key ID: EFD315F31848DBA4
6 changed files with 82 additions and 19 deletions

View file

@ -29,22 +29,66 @@
(require 'ert)
(require 'h)
; For reference: test-root-1 looks like this
; test-root-1
; ├── example1.tld
; │ ├── user1
; │ │ ├── proj1
; │ │ └── proj2
; │ └── user2
; │ └── proj1
; └── example2.tld
; └── user1
; └── proj1
(ert-deftest h-get-code-root-projects-test ()
; Test Helpers
;;;;;;;;;;;;;;
(defun h--tests-with-temp-dir (func)
"Run the FUNC function in a temporary directory.
FUNC gets called with the temp dir as parameter.
The directory gets deleted once we exit FUNC."
(let ((temp-dir (make-temp-file "h-test-" t)))
(unwind-protect
(funcall func (file-name-as-directory temp-dir))
(delete-directory temp-dir t))))
(defun h--tests-init-fake-git-repo (dir)
"Create a dummy git repo at DIR.
If DIR doesn't exists, we create it first."
(let ((d (file-name-as-directory dir)))
(progn
(unless (file-directory-p d) (make-directory d t))
(h--call-git-in-dir d "init" ))))
; Test Dirs Setup
;;;;;;;;;;;;;;;;;
(defun h--tests-run-on-testroot-1 (func)
"Run the FUNC function on testroot1.
FUNC is called with the directory cotaining test root 1 as parameter.
For reference: test-root-1 looks like this:
test-root-1
example1.tld
user1
proj1
proj2
user2
proj1
example2.tld
user1
proj1"
(h--tests-with-temp-dir
(lambda (temp-dir)
(progn
(h--tests-init-fake-git-repo (concat temp-dir "/example1.tld/user1/proj1"))
(h--tests-init-fake-git-repo (concat temp-dir "/example1.tld/user1/proj2"))
(h--tests-init-fake-git-repo (concat temp-dir "/example1.tld/user2/proj1"))
(h--tests-init-fake-git-repo (concat temp-dir "/example2.tld/user1/proj1"))
(funcall func temp-dir)
))))
; Tests
;;;;;;;
(ert-deftest h--tests-get-code-root-projects ()
"Testing the `h--get-code-root-projects with test-root-1 setup."
(let
(
(results (h--get-code-root-projects "./test/fixtures/test-root-1")))
((results
(h--tests-run-on-testroot-1 (lambda (root) (h--get-code-root-projects root))))
)
(should (member "example1.tld/user1/proj1" results))
(should (member "example1.tld/user1/proj2" results))
(should (member "example1.tld/user2/proj1" results))
@ -54,6 +98,7 @@
(h--get-code-root-projects "./test/fixtures/test-root-1")
(file-name-as-directory "~/code-root")

24
h.el
View file

@ -47,6 +47,28 @@ local directory"
:type 'directory
:group 'h-group)
(defcustom h-git-bin "git"
"Path pointing to the git binary.
By default, it'll look for git in the current $PATH."
:type 'file
:group 'h-group)
(defun h--git-path ()
"Find the git binary path using `h-git-bin`.
Errors out if we can't find it."
(if (file-executable-p h-git-bin)
h-git-bin
(let ((git-from-bin-path (locate-file h-git-bin exec-path)))
(if (file-executable-p git-from-bin-path)
git-from-bin-path
(error "Can't find git. Is h-git-bin correctly set?")))))
(defun h--call-git-in-dir (dir args)
"Call the git binary as pointed by `h-git-bin` in DIR with ARGS."
(let ((default-directory dir))
(process-file (h--git-path) nil nil nil args)))
(defun h--is-git-repo (dir)
"Check if DIR is a git repo using a pretty weak heuristic."
(file-directory-p (concat (file-name-as-directory dir) ".git")))
@ -62,7 +84,7 @@ any further."
(let*
((is-not-git-repo (lambda (dir) (not (h--is-git-repo dir))))
(remove-code-root-prefix
(lambda (path) (string-remove-prefix (concat code-root "/") path)))
(lambda (path) (string-remove-prefix (concat (file-name-as-directory code-root)) path)))
;;; PERF: Using directory-files-recursively is pretty
;;; inneficient. We have to list the dir content twice:
;;; 1. when directory-files-recursively checks.

@ -1 +0,0 @@
Subproject commit 6943ed79983734fe9ad628e2feefbda62a8130bb

@ -1 +0,0 @@
Subproject commit 99929c82f82047e662aa5784dc99f6a380b45f19

@ -1 +0,0 @@
Subproject commit 99929c82f82047e662aa5784dc99f6a380b45f19

@ -1 +0,0 @@
Subproject commit b4f7fc570ad41b68e772f15311df4ad93f468a23