2022-04-01 09:55:31 +02:00
|
|
|
;;; h-tests.el --- Project navigation and remote checkout -*- lexical-binding: t; -*-
|
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;;; Copyright (C) 2022 Félix Baylac Jacqué
|
|
|
|
;;; Author: Félix Baylac Jacqué <felix at alternativebit.fr>
|
|
|
|
;;; Maintainer: Félix Baylac Jacqué <felix at alternativebit.fr>
|
|
|
|
;;; Version: 1.14.0
|
2022-04-01 09:55:31 +02:00
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;;; (at your option) any later version.
|
2022-04-01 09:55:31 +02:00
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;;; This program is distributed in the hope that it will be useful,
|
|
|
|
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
2022-04-01 09:55:31 +02:00
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <https://www.gnu.org/licenses/>
|
2022-04-01 09:55:31 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;;; TODO before publish
|
2022-04-01 09:55:31 +02:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'ert)
|
|
|
|
(require 'h)
|
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;; Test Helpers
|
2022-04-02 11:56:24 +02:00
|
|
|
;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(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))
|
2022-05-05 11:53:43 +02:00
|
|
|
(h--call-git-in-dir d "init"))))
|
2022-04-02 11:56:24 +02:00
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;; Test Dirs Setup
|
2022-04-02 11:56:24 +02:00
|
|
|
;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(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)
|
|
|
|
))))
|
|
|
|
|
2022-04-03 17:48:10 +02:00
|
|
|
(defun h--tests-run-on-testroot-2 (func)
|
|
|
|
"Run the FUNC function on testroot2.
|
|
|
|
|
|
|
|
FUNC is called with the directory cotaining test root 2 as parameter.
|
|
|
|
|
|
|
|
For reference: test-root-2 looks like this:
|
|
|
|
test-root-2
|
|
|
|
├── example1.tld
|
|
|
|
│ ├── user1
|
|
|
|
│ │ ├── proj1
|
|
|
|
│ │ └── proj2 (NOT A GIT REPO)
|
|
|
|
│ └── 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"))
|
|
|
|
(make-directory (concat (file-name-as-directory 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)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defun h--tests-run-on-empty-testroot (func)
|
|
|
|
"Run the FUNC function on testroot1.
|
|
|
|
|
|
|
|
FUNC is called with a empty test root.
|
|
|
|
|
|
|
|
For reference: a empty test root looks like this:
|
|
|
|
test-root"
|
|
|
|
(h--tests-with-temp-dir
|
|
|
|
(lambda (temp-dir)
|
|
|
|
(progn
|
|
|
|
(funcall func temp-dir)))))
|
|
|
|
|
2022-04-04 17:31:26 +02:00
|
|
|
;; Tests
|
2022-04-02 11:56:24 +02:00
|
|
|
;;;;;;;
|
|
|
|
|
2022-04-03 17:48:10 +02:00
|
|
|
(ert-deftest h--tests-get-code-root-projects-coderoot-1 ()
|
|
|
|
"Test the `h--get-code-root-projects with test-root-1 setup."
|
2022-04-01 20:04:09 +02:00
|
|
|
(let
|
2022-04-02 11:56:24 +02:00
|
|
|
((results
|
|
|
|
(h--tests-run-on-testroot-1 (lambda (root) (h--get-code-root-projects root))))
|
|
|
|
)
|
2022-04-01 20:04:09 +02:00
|
|
|
(should (member "example1.tld/user1/proj1" results))
|
|
|
|
(should (member "example1.tld/user1/proj2" results))
|
|
|
|
(should (member "example1.tld/user2/proj1" results))
|
|
|
|
(should (member "example2.tld/user1/proj1" results))
|
2022-04-03 17:48:10 +02:00
|
|
|
(should (eq (length results) 4))))
|
2022-04-01 20:04:09 +02:00
|
|
|
|
2022-04-06 19:24:12 +02:00
|
|
|
|
|
|
|
(ert-deftest h--tests-find-git-dirs-recursively-coderoot-1 ()
|
|
|
|
"Test the `h--get-code-root-projects with test-root-1 setup."
|
|
|
|
(let*
|
|
|
|
((r nil)
|
|
|
|
(results
|
|
|
|
(h--tests-run-on-testroot-1
|
|
|
|
(lambda (root)
|
|
|
|
(progn (setq r root)
|
|
|
|
(h--find-git-dirs-recursively root))))))
|
|
|
|
(should (member (concat r "example1.tld/user1/proj1/") results))
|
|
|
|
(should (member (concat r "example1.tld/user1/proj2/") results))
|
|
|
|
(should (member (concat r "example1.tld/user2/proj1/") results))
|
|
|
|
(should (member (concat r "example2.tld/user1/proj1/") results))
|
|
|
|
(should (eq (length results) 4))))
|
|
|
|
|
2022-04-03 17:48:10 +02:00
|
|
|
(ert-deftest h--tests-get-code-root-projects-coderoot-2 ()
|
|
|
|
"Test the `h--get-code-root-projects with test-root-2 setup."
|
|
|
|
(let
|
|
|
|
((results
|
|
|
|
(h--tests-run-on-testroot-2 (lambda (root) (h--get-code-root-projects root))))
|
|
|
|
)
|
|
|
|
(should (member "example1.tld/user1/proj1" results))
|
|
|
|
(should (member "example1.tld/user2/proj1" results))
|
|
|
|
(should (member "example2.tld/user1/proj1" results))
|
|
|
|
(should (eq (length results) 3))))
|
2022-04-01 20:04:09 +02:00
|
|
|
|
2022-04-06 19:24:12 +02:00
|
|
|
(ert-deftest h--tests-find-git-dirs-recursively-coderoot-2 ()
|
|
|
|
"Test the `h--get-code-root-projects with test-root-2 setup."
|
|
|
|
(let*
|
|
|
|
((r nil)
|
|
|
|
(results
|
|
|
|
(h--tests-run-on-testroot-2
|
|
|
|
(lambda (root)
|
|
|
|
(progn (setq r root)
|
|
|
|
(h--find-git-dirs-recursively root))))))
|
|
|
|
(should (member (concat r "example1.tld/user1/proj1/") results))
|
|
|
|
(should (member (concat r "example1.tld/user2/proj1/") results))
|
|
|
|
(should (member (concat r "example2.tld/user1/proj1/") results))
|
|
|
|
(should (eq (length results) 3))))
|
|
|
|
|
2022-04-03 17:48:10 +02:00
|
|
|
(ert-deftest h--tests-get-code-root-projects-empty-coderoot ()
|
|
|
|
"Test the `h--get-code-root-projects with a empty coderoot."
|
|
|
|
(let
|
|
|
|
((results
|
|
|
|
(h--tests-run-on-empty-testroot (lambda (root) (h--get-code-root-projects root))))
|
|
|
|
)
|
|
|
|
(should (seq-empty-p results))))
|
2022-04-01 20:04:09 +02:00
|
|
|
|
2022-04-06 19:24:12 +02:00
|
|
|
(ert-deftest h--tests-find-git-dirs-recursively-empty-coderoot ()
|
|
|
|
"Test the `h--get-code-root-projects with a empty coderoot."
|
|
|
|
(let
|
|
|
|
((results
|
|
|
|
(h--tests-run-on-empty-testroot (lambda (root) (h--find-git-dirs-recursively root))))
|
|
|
|
)
|
|
|
|
(should (seq-empty-p results))))
|
|
|
|
|
2022-04-03 17:48:10 +02:00
|
|
|
(ert-deftest h--tests-get-code-root-projects-no-coderoot ()
|
|
|
|
"Test the `h--get-code-root-projects with a non-existing coderoot."
|
|
|
|
(let
|
|
|
|
((results (h--get-code-root-projects "/does/not/exist")))
|
|
|
|
(should (seq-empty-p results))))
|
2022-04-01 09:55:31 +02:00
|
|
|
|
2022-04-07 11:57:17 +02:00
|
|
|
|
|
|
|
;; Test Fetchers
|
|
|
|
;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; Github
|
|
|
|
|
2022-04-08 16:25:16 +02:00
|
|
|
(ert-deftest h--tests-fetch-github-parse-response-ok ()
|
2022-04-07 11:57:17 +02:00
|
|
|
"Test h--tests-fetch-github-parse-response with a fixture."
|
|
|
|
(with-temp-buffer
|
2022-04-08 16:25:16 +02:00
|
|
|
(insert-file-contents "./tests/fixtures/github-get-request-ok.txt")
|
2022-04-07 11:57:17 +02:00
|
|
|
(should (equal (h--fetch-github-parse-response (current-buffer))
|
|
|
|
'((ssh . "git@github.com:NinjaTrappeur/h.el.git")
|
2022-06-09 12:13:49 +02:00
|
|
|
(https . "https://github.com/NinjaTrappeur/h.el.git"))))))
|
2022-04-07 11:57:17 +02:00
|
|
|
|
2022-04-08 16:25:16 +02:00
|
|
|
(ert-deftest h--tests-fetch-github-parse-response-ko ()
|
|
|
|
"Test h--tests-fetch-github-parse-response with a fixture."
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents "./tests/fixtures/github-get-request-ko.txt")
|
|
|
|
(should (equal (h--fetch-github-parse-response (current-buffer)) nil))))
|
|
|
|
|
2022-06-16 18:59:45 +02:00
|
|
|
;; Gitea
|
|
|
|
|
|
|
|
(ert-deftest h--tests-fetch-gitea-parse-response-ok ()
|
|
|
|
"Test h--tests-fetch-gitea-parse-response with a fixture."
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents "./tests/fixtures/gitea-get-request-ok.txt")
|
|
|
|
(should (equal (h--fetch-gitea-parse-response (current-buffer))
|
|
|
|
'((ssh . "gitea@git.alternativebit.fr:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://git.alternativebit.fr/NinjaTrappeur/h.el.git"))))))
|
|
|
|
|
|
|
|
(ert-deftest h--tests-fetch-gitea-parse-response-ko ()
|
|
|
|
"Test h--tests-fetch-gitea-parse-response with a fixture."
|
|
|
|
(with-temp-buffer
|
|
|
|
(insert-file-contents "./tests/fixtures/gitea-get-request-ko.txt")
|
|
|
|
(should (equal (h--fetch-gitea-parse-response (current-buffer)) nil))))
|
|
|
|
|
2022-05-04 20:04:20 +02:00
|
|
|
;; Test repo URI parser
|
|
|
|
;;;;;;;;;;;;;;;;;
|
|
|
|
|
2022-05-19 11:57:31 +02:00
|
|
|
(ert-deftest h--test-parse-repo-identifier ()
|
2022-05-04 20:04:20 +02:00
|
|
|
"Test h--parse-repo-identifier."
|
2022-05-19 11:57:31 +02:00
|
|
|
(should (equal
|
|
|
|
(h--parse-repo-identifier "https://github.com/Ninjatrappeur/h.el")
|
|
|
|
'((tag . full-url) (full-url . "https://github.com/Ninjatrappeur/h.el"))))
|
|
|
|
(should (equal
|
|
|
|
(h--parse-repo-identifier "github.com/Ninjatrappeur/h.el")
|
|
|
|
'((tag . full-url) (full-url . "github.com/Ninjatrappeur/h.el"))))
|
|
|
|
(should (equal
|
|
|
|
(h--parse-repo-identifier "Ninjatrappeur/h.el")
|
|
|
|
'((tag . owner-repo) (owner . "Ninjatrappeur") (repo . "h.el"))))
|
|
|
|
(should (equal
|
|
|
|
(h--parse-repo-identifier "h.el")
|
|
|
|
'((tag . repo) (repo . "h.el")))))
|
2022-05-04 20:04:20 +02:00
|
|
|
|
2022-05-04 20:07:00 +02:00
|
|
|
(ert-deftest h--test-filepath-from-clone-url ()
|
|
|
|
"Test h--filepath-from-clone-url."
|
|
|
|
;; HTTP/HTTPS
|
|
|
|
(should (equal (h--filepath-from-clone-url "http://github.com/NinjaTrappeur/h.el.git") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
(should (equal (h--filepath-from-clone-url "http://github.com/NinjaTrappeur/h.el") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
(should (equal (h--filepath-from-clone-url "https://github.com/NinjaTrappeur/h.el.git") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
(should (equal (h--filepath-from-clone-url "https://github.com/NinjaTrappeur/h.el") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
;; SSH
|
|
|
|
(should (equal (h--filepath-from-clone-url "ssh://git@github.com:NinjaTrappeur/h.el.git") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
(should (equal (h--filepath-from-clone-url "ssh://git@github.com:NinjaTrappeur/h.el") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
(should (equal (h--filepath-from-clone-url "git@github.com:NinjaTrappeur/h.el.git") "github.com/NinjaTrappeur/h.el"))
|
|
|
|
(should (equal (h--filepath-from-clone-url "git@github.com:NinjaTrappeur/h.el") "github.com/NinjaTrappeur/h.el")))
|
|
|
|
|
2022-05-05 11:53:43 +02:00
|
|
|
(ert-deftest h--test-git-clone-in-dir ()
|
2022-05-16 18:40:42 +02:00
|
|
|
"Test the h--git-clone-in-dir function."
|
2022-05-05 11:53:43 +02:00
|
|
|
(h--tests-run-on-testroot-1
|
|
|
|
(lambda (dir)
|
|
|
|
(let
|
|
|
|
((tmpdir (make-temp-file "h-test-" t)))
|
|
|
|
(progn
|
|
|
|
(h--git-clone-in-dir
|
|
|
|
(format "file://%s" (concat dir "example1.tld/user1/proj1/"))
|
|
|
|
tmpdir)
|
|
|
|
(should (file-exists-p (format "%s/.git" tmpdir)))
|
|
|
|
(delete-directory tmpdir t))))))
|
|
|
|
|
2022-05-16 18:40:42 +02:00
|
|
|
(ert-deftest h--test-pick-relevant-forges ()
|
|
|
|
"Test the h--pick-relevant-forges function."
|
|
|
|
(let
|
|
|
|
((forge-list
|
2022-06-05 08:11:51 +02:00
|
|
|
'((forge1 . ((query-user-repo . h--query-github)
|
|
|
|
(url . "https://forge1.com/.*/.*")))
|
|
|
|
(forge2 . ((query-user-repo . h--query-github)
|
|
|
|
(url . "https://forge2.com/.*/.*"))))))
|
2022-05-16 18:40:42 +02:00
|
|
|
(should (equal (h--pick-relevant-forges "owner/repo" forge-list) forge-list))
|
|
|
|
(should (equal (h--pick-relevant-forges "repo" forge-list) forge-list))
|
|
|
|
(should (equal
|
|
|
|
(h--pick-relevant-forges "https://forge1.com/owner/repo" forge-list)
|
2022-06-05 08:11:51 +02:00
|
|
|
'((forge1 . ((query-user-repo . h--query-github)
|
|
|
|
(url . "https://forge1.com/.*/.*"))))))))
|
2022-05-16 18:40:42 +02:00
|
|
|
|
2022-06-09 12:17:16 +02:00
|
|
|
;;; UI-related tests
|
|
|
|
|
|
|
|
(ert-deftest h--test-add-keys-to-forge-status ()
|
|
|
|
"Test the h--add-keys-to-forge-status function."
|
|
|
|
(let
|
|
|
|
((dummy-forge-query-status-one-result
|
|
|
|
'(("GitHub"
|
|
|
|
(ssh . "git@github.com:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://github.com/NinjaTrappeur/h.el.git"))
|
|
|
|
("GitLab" . not-found)))
|
|
|
|
(expected-forge-query-status-with-keys-one-result
|
|
|
|
`(("GitHub"
|
|
|
|
(status
|
|
|
|
(ssh . "git@github.com:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://github.com/NinjaTrappeur/h.el.git"))
|
|
|
|
(key . ,?1))
|
|
|
|
("GitLab" (status . not-found))))
|
|
|
|
(dummy-forge-query-status-two-results
|
|
|
|
'(("GitHub"
|
|
|
|
(ssh . "git@github.com:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://github.com/NinjaTrappeur/h.el.git"))
|
|
|
|
("Codeberg" . not-found)
|
|
|
|
("GitLab"
|
|
|
|
(ssh . "git@gitlab.com:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://gitlab.com/NinjaTrappeur/h.el.git"))))
|
|
|
|
(expected-forge-query-status-with-keys-two-results
|
|
|
|
`(("GitHub"
|
|
|
|
(status
|
|
|
|
(ssh . "git@github.com:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://github.com/NinjaTrappeur/h.el.git"))
|
|
|
|
(key . ,'?1))
|
|
|
|
("Codeberg" (status . not-found))
|
|
|
|
("GitLab"
|
|
|
|
(status
|
|
|
|
(ssh . "git@gitlab.com:NinjaTrappeur/h.el.git")
|
|
|
|
(https . "https://gitlab.com/NinjaTrappeur/h.el.git"))
|
|
|
|
(key . ,'?2)))))
|
|
|
|
|
|
|
|
(should (equal
|
|
|
|
expected-forge-query-status-with-keys-one-result
|
|
|
|
(h--add-keys-to-forge-status dummy-forge-query-status-one-result)))
|
|
|
|
(should (equal
|
|
|
|
expected-forge-query-status-with-keys-two-results
|
|
|
|
(h--add-keys-to-forge-status dummy-forge-query-status-two-results)))
|
|
|
|
))
|
|
|
|
|
2022-04-01 09:55:31 +02:00
|
|
|
(provide 'h-tests)
|
|
|
|
;;; h-tests.el ends here
|