git.fiddlerwoaroof.com
Browse code

feat(tests): add tests for pack files

Ed L authored on 17/11/2020 08:08:29
Showing 7 changed files
... ...
@@ -56,7 +56,7 @@
56 56
     (list (cons :type (object-type->sym type))
57 57
           (cons :decompressed-size size)
58 58
           (cons :object-data object-data)
59
-          (cons :raw-data object-data))))
59
+          (cons :raw-data decompressed))))
60 60
 
61 61
 (defun extract-object-of-type (type s repository pos)
62 62
   (with-simple-restart (continue "Skip object of type ~s" type)
... ...
@@ -98,15 +98,16 @@
98 98
 (defun pack (index pack repository)
99 99
   (fw.lu:new 'pack index pack repository))
100 100
 
101
-(defun pack-files (repo)
102
-  (mapcar (serapeum:op
103
-            (pack _1
104
-                  (merge-pathnames
105
-                   (make-pathname :type "pack") _1)
106
-                  (repository repo)))
107
-          (uiop:directory*
108
-           (merge-pathnames ".git/objects/pack/*.idx"
109
-                            repo))))
101
+(defgeneric pack-files (repo)
102
+  (:method ((repo git-repository))
103
+    (mapcar (serapeum:op
104
+              (pack _1
105
+                    (merge-pathnames
106
+                     (make-pathname :type "pack") _1)
107
+                    repo))
108
+            (uiop:directory*
109
+             (merge-pathnames ".git/objects/pack/*.idx"
110
+                              (root-of repo))))))
110 111
 
111 112
 (defgeneric loose-object (repository id)
112 113
   (:method ((repository string) id)
... ...
@@ -138,6 +139,8 @@
138 139
   (print-unreadable-object (obj s :type t)
139 140
     (format s "~a of ~a"
140 141
             (subseq (ref-hash obj) 0 7)
142
+            (ref-repo obj)
143
+            #+(or)
141 144
             (serapeum:string-replace (namestring (user-homedir-pathname))
142 145
                                      (root-of (ref-repo obj))
143 146
                                      "~/"))))
... ...
@@ -6,6 +6,15 @@
6 6
     ((or pathname string) (namestring
7 7
                            (truename repo)))))
8 8
 
9
+(defun packed-ref (repo id)
10
+  (multiple-value-bind (pack offset) (find-object-in-pack-files repo id)
11
+    (when pack
12
+      (make-instance 'packed-ref
13
+                     :hash id
14
+                     :repo repo
15
+                     :offset offset
16
+                     :pack pack))))
17
+
9 18
 (defgeneric ref (repo id)
10 19
   (:documentation "Given a REPOsitory and a ref ID return the ref-id object.")
11 20
   (:method ((repo git-repository) (id string))
... ...
@@ -15,13 +24,7 @@
15 24
                            :repo repo-root
16 25
                            :hash id
17 26
                            :file object-file))
18
-          (multiple-value-bind (pack offset) (find-object-in-pack-files repo-root id)
19
-            (when pack
20
-              (make-instance 'packed-ref
21
-                             :hash id
22
-                             :repo repo-root
23
-                             :offset offset
24
-                             :pack pack)))))))
27
+          (packed-ref repo id)))))
25 28
 
26 29
 (defun ensure-ref (thing &optional (repo *git-repository*))
27 30
   (typecase thing
... ...
@@ -3,6 +3,7 @@
3 3
   (:export ))
4 4
 (in-package :fwoar.cl-git.git-objects)
5 5
 
6
+(defparameter *fake-repo* :fwoar.cl-git.git-objects)
6 7
 (fiveam:def-suite :fwoar.cl-git.git-objects
7 8
   :description "testing branch resolution"
8 9
   :in :fwoar.cl-git)
... ...
@@ -11,12 +12,12 @@
11 12
 (fw.lu:defclass+ fake-ref ()
12 13
   ((%repo :initarg :repo :reader repo)
13 14
    (%hash :initarg :hash :reader hash)))
14
-(defmethod fwoar.cl-git::ref ((repo (eql :the-repo)) hash)
15
+(defmethod fwoar.cl-git::ref ((repo (eql *fake-repo*)) hash)
15 16
   (fake-ref repo hash))
16 17
 
17 18
 
18 19
 (fiveam:def-test basic-commit ()
19
-  (let ((fwoar.cl-git::*git-repository* :the-repo)
20
+  (let ((fwoar.cl-git::*git-repository* *fake-repo*)
20 21
         (object (fwoar.cl-git::extract-loose-object
21 22
                  nil
22 23
                  (asdf:system-relative-pathname
... ...
@@ -36,7 +37,7 @@
36 37
                    (fwoar.cl-git:component :parents object)))
37 38
     (5am:is (equal "1da546ab4697b719efb62f11fd785d6ad3b226d2"
38 39
                    (hash (fwoar.cl-git:component :tree object))))
39
-    (5am:is (equal :the-repo
40
+    (5am:is (equal *fake-repo*
40 41
                    (repo (fwoar.cl-git:component :tree object))))
41 42
     (5am:is (equal '(("author" "L Edgley <foo@bar.com> 1605513585 -0800")
42 43
                      ("committer" "Ed L <el-github@elangley.org> 1605513585 -0800")
... ...
@@ -62,3 +63,90 @@
62 63
                      (fwoar.cl-git:component :name entry)))
63 64
       (5am:is (equal "100644"
64 65
                      (fwoar.cl-git:component :mode entry))))))
66
+
67
+(defparameter *fake-repo* :fwoar.cl-git.git-objects.pack)
68
+(defmethod fwoar.cl-git::ref ((repo (eql *fake-repo*)) hash)
69
+  (fake-ref repo hash))
70
+(defmethod fwoar.cl-git::pack-files ((repo (eql *fake-repo*)))
71
+  (list
72
+   (fwoar.cl-git::pack (asdf:system-relative-pathname
73
+                        :cl-git
74
+                        "tests/sample-git-objects/hello-world-pack.idx")
75
+                       (asdf:system-relative-pathname
76
+                        :cl-git
77
+                        "tests/sample-git-objects/hello-world-pack.pack")
78
+                       repo)))
79
+
80
+(fiveam:def-test pack-files-commit ()
81
+  (let* ((hash "7d7b56a6a64e090041f55293511f48aba6699f1a")
82
+         (ref (fwoar.cl-git::packed-ref-for-object
83
+               :fwoar.cl-git.git-objects.pack
84
+               hash))
85
+         (object (progn (fiveam:is (not (null ref)))
86
+                        (fiveam:is (equal hash (fwoar.cl-git::ref-hash ref)))
87
+                        (fiveam:is (equal *fake-repo* (fwoar.cl-git::ref-repo ref)))
88
+                        (fwoar.cl-git::extract-object ref))))
89
+
90
+    (5am:is (typep object 'fwoar.cl-git::git-commit))
91
+    (5am:is (equal "hello, git!
92
+"
93
+                   (fwoar.cl-git:component :message object)))
94
+    (5am:is (equal ()
95
+                   (fwoar.cl-git:component :parents object)))
96
+    (5am:is (equal "L Edgley <foo@bar.com> 1605513585 -0800"
97
+                   (fwoar.cl-git:component :author object)))
98
+    (5am:is (equal "Ed L <el-github@elangley.org> 1605513585 -0800"
99
+                   (fwoar.cl-git:component :committer object)))
100
+    (5am:is (equal ()
101
+                   (fwoar.cl-git:component :parents object)))
102
+    (let ((fwoar.cl-git::*git-repository* *fake-repo*))
103
+      (5am:is (equal "1da546ab4697b719efb62f11fd785d6ad3b226d2"
104
+                     (hash (fwoar.cl-git:component :tree object))))
105
+      (5am:is (equal *fake-repo*
106
+                     (repo (fwoar.cl-git:component :tree object)))))
107
+    (5am:is (equal '(("author" "L Edgley <foo@bar.com> 1605513585 -0800")
108
+                     ("committer" "Ed L <el-github@elangley.org> 1605513585 -0800")
109
+                     ("tree" "1da546ab4697b719efb62f11fd785d6ad3b226d2"))
110
+                   (coerce (sort (copy-seq (fwoar.cl-git::metadata object))
111
+                                 'string-lessp
112
+                                 :key 'car)
113
+                           'list)))))
114
+
115
+(fiveam:def-test pack-files-tree ()
116
+  (let* ((hash "1da546ab4697b719efb62f11fd785d6ad3b226d2")
117
+         (ref (fwoar.cl-git::packed-ref-for-object
118
+               :fwoar.cl-git.git-objects.pack
119
+               hash))
120
+         (object (progn (fiveam:is (not (null ref)))
121
+                        (fiveam:is (equal hash (fwoar.cl-git::ref-hash ref)))
122
+                        (fiveam:is (equal *fake-repo* (fwoar.cl-git::ref-repo ref)))
123
+                        (fwoar.cl-git::extract-object ref))))
124
+    (5am:is (typep object 'fwoar.cl-git::git-tree))
125
+    (let* ((entries (fwoar.cl-git::entries object))
126
+           (entry (progn (5am:is (= (length entries) 1))
127
+                         (car entries))))
128
+      (5am:is (equal "4b5fa63702dd96796042e92787f464e28f09f17d"
129
+                     (fwoar.cl-git:component :hash entry)))
130
+      (5am:is (equal "a"
131
+                     (fwoar.cl-git:component :name entry)))
132
+      (5am:is (equal "100644"
133
+                     (fwoar.cl-git:component :mode entry))))))
134
+
135
+(fiveam:def-test pack-files-blob ()
136
+  (let* ((hash "4b5fa63702dd96796042e92787f464e28f09f17d")
137
+         (ref (fwoar.cl-git::packed-ref-for-object
138
+               :fwoar.cl-git.git-objects.pack
139
+               hash))
140
+         (object (progn (fiveam:is (not (null ref)))
141
+                        (fiveam:is (equal hash (fwoar.cl-git::ref-hash ref)))
142
+                        (fiveam:is (equal *fake-repo* (fwoar.cl-git::ref-repo ref)))
143
+                        (fwoar.cl-git::extract-object ref))))
144
+    (5am:is (typep object '(vector (unsigned-byte 8))))
145
+    (5am:is (equal "hello, world
146
+"
147
+                   (babel:octets-to-string
148
+                    (fwoar.cl-git::extract-object
149
+                     (fwoar.cl-git::packed-ref-for-object
150
+                      :fwoar.cl-git.git-objects.pack
151
+                      "4b5fa63702dd96796042e92787f464e28f09f17d"))
152
+                    :encoding :utf-8)))))
65 153
new file mode 100644
66 154
Binary files /dev/null and b/tests/sample-git-objects/hello-world-pack.idx differ
67 155
new file mode 100644
68 156
Binary files /dev/null and b/tests/sample-git-objects/hello-world-pack.pack differ
69 157
new file mode 100644
... ...
@@ -0,0 +1,5 @@
1
+7d7b56a6a64e090041f55293511f48aba6699f1a commit 163 137 12
2
+4b5fa63702dd96796042e92787f464e28f09f17d blob   13 22 149
3
+1da546ab4697b719efb62f11fd785d6ad3b226d2 tree   29 40 171
4
+non delta: 3 objects
5
+.git/objects/pack/pack-9560f994ee4405c39b0eb9857c81c764aa96323a.pack: ok