Permalink
Browse files

improved random file modification and commit generator

  • Loading branch information...
1 parent 234f44b commit 2a16e26b20a3834f316a3ac262c7aca35786653d @russell committed Apr 17, 2012
Showing with 93 additions and 49 deletions.
  1. +1 −1 run-tests
  2. +92 −48 tests.lisp
View
@@ -9,4 +9,4 @@
(FiveAM:run! (list 'repository-init
'create-commit
- 'create-commits))
+ 'create-random-commits))
View
@@ -4,10 +4,10 @@
:common-lisp
:cl-git
:it.bese.FiveAM)
- (:export
- #:repository-init
- #:create-commit
- #:create-commits))
+ (:export
+ #:repository-init
+ #:create-commit
+ #:create-random-commits))
(in-package #:cl-git-tests)
@@ -22,32 +22,73 @@
(gen-string :length (gen-integer :min 5 :max 10)
:elements (gen-letter)))))
+(defun random-string (length)
+ (funcall
+ (gen-string :length (gen-integer :min length :max length)
+ :elements (gen-letter))))
+
+(defun assoc-default (key alist)
+ (cdr (assoc key alist)))
+
(test repository-init
- "create a repository and open it to make sure that works"
- (for-all ((path 'gen-temp-path))
- (finishes
- (unwind-protect
- (progn
- (cl-git:git-repository-init path :bare)
- (cl-git:git-repository-open path)
- (cl-git:git-repository-free))
- (progn
- (cl-fad:delete-directory-and-files path))))))
+ "create a repository and open it to make sure that works"
+ (for-all ((path 'gen-temp-path))
+ (finishes
+ (unwind-protect
+ (progn
+ (cl-git:git-repository-init path :bare)
+ (cl-git:git-repository-open path)
+ (cl-git:git-repository-free))
+ (progn
+ (cl-fad:delete-directory-and-files path))))))
(defun add-random-file-modification (repo-path filename)
- (let ((test-file (concatenate 'string repo-path "/" filename)))
- (with-open-file (stream test-file :direction :output :if-exists :supersede)
- (format stream "Random text: ~s.\n"
- (funcall
- (gen-string :length (gen-integer :min 5 :max 10)
- :elements (gen-letter)))))
- (cl-git:git-index-add filename)
- (cl-git:git-index-write)))
-
-(defun commit-random-file-modification (repo-path filename commit-message &key (parents nil))
+ "randomly modify the file in the repo at repo-path."
+ (let ((test-file (concatenate 'string repo-path "/" filename))
+ (content (with-output-to-string (stream)
+ (format stream "Random text: ~A.\n" (random-string 100)))))
+ (with-open-file (stream test-file :direction :output :if-exists :supersede)
+ content)
+ (cl-git:git-index-add filename)
+ (cl-git:git-index-write)
+ content))
+
+(defun add-new-random-file (repo-path)
+ "add a new random file to the index of the repo located at
+REPO-PATH."
+ (let ((filename (random-string 25)))
+ (cl-git:with-git-repository-index
+ (let ((content (add-random-file-modification repo-path filename)))
+ `((filename . ,filename)(content . ,content))))))
+
+(defun commit-random-file (repo-path &key (parents nil))
+ "create a new random file in the repository located at REPO-PATH
+then add and commit it to the repository. returns a list containing
+commit-message filename content."
+ (let ((commit-message (with-output-to-string (stream)
+ (format
+ stream "Random commit: ~A.\n"
+ (random-string 100)))))
+ (cl-git:with-git-repository-index
+ (let* ((file (add-new-random-file repo-path))
+ (commit-sha (cl-git:git-commit-create
+ (cl-git:git-oid-from-index)
+ commit-message
+ :parents parents)))
+ (cons `(commit-sha . ,commit-sha)
+ (cons `(commit-message . ,commit-message)
+ file))))))
+
+(defun commit-random-file-modification (repo-path
+ filename
+ commit-message
+ &key (parents nil))
(cl-git:with-git-repository-index
(add-random-file-modification repo-path filename)
- (cl-git:git-commit-create (cl-git:git-oid-from-index) commit-message :parents parents)))
+ (cl-git:git-commit-create
+ (cl-git:git-oid-from-index)
+ commit-message
+ :parents parents)))
(test create-commit
"create a repository and add a file to it."
@@ -63,33 +104,36 @@
path "test" "Test commit"))
(is (equal (cl-git:git-commit-message commit)
"Test commit"))))))
- (progn
- (cl-fad:delete-directory-and-files path)))))
+ (progn
+ (cl-fad:delete-directory-and-files path)))))
-(test create-commits
- "create a repository and add several commits to it."
+
+(defun create-random-commits (repo-path number)
+ "create a number of random commits to random files."
+ (if (> 1 (1- number))
+ (let ((new-commit (commit-random-file repo-path)))
+ (cons new-commit nil))
+ (let* ((commit (create-random-commits repo-path (1- number)))
+ (new-commit (commit-random-file repo-path :parents (assoc-default 'commit-sha (car commit)))))
+ (cons new-commit commit))))
+
+
+(test create-random-commits
+ "create a repository and add several random commits to it. then
+check that the commit messages match the expected messages."
(let ((path (gen-temp-path)))
(finishes
(unwind-protect
(progn
(cl-git:git-repository-init path)
(cl-git:with-git-repository (path)
- (let ((commit-sha
- (commit-random-file-modification
- path "test" "Test commit3"
- :parents
- (commit-random-file-modification
- path "test" "Test commit2"
- :parents
- (commit-random-file-modification
- path "test" "Test commit1")))))
- (let ((commit-messages (list "Test commit3"
- "Test commit2"
- "Test commit1")))
- (cl-git:with-git-revisions (commit :sha commit-sha)
- (is (equal (cl-git:git-commit-message commit)
- (car commit-messages)))
- (setq commit-messages (cdr commit-messages))))))))
- (progn
- (cl-fad:delete-directory-and-files path)
- ))))
+ (create-random-commits path 10))
+ (cl-git:with-git-repository (path)
+ (let* ((commit-list (create-random-commits path 10))
+ (tcommit (pop commit-list)))
+ (cl-git:with-git-revisions (commit :sha (assoc-default 'commit-sha tcommit))
+ (is (equal (cl-git:git-commit-message commit)
+ (assoc-default 'commit-message tcommit)))
+ (setq tcommit (pop commit-list))))))
+ (progn
+ (cl-fad:delete-directory-and-files path))))))

0 comments on commit 2a16e26

Please sign in to comment.