2015-09-21 101 views
0

我有一個Ruby腳本,用於檢查git repo和url端點上的文件之間的變化。如果存在更改,它會提交併推送到回購站。我想爲此腳本編寫rspec測試,但是我無法理解如何執行此操作,因爲我不希望測試將任何事情推送到實際的Git倉庫。我想我需要建立某種模擬存儲庫,但我不完全確定。任何有關如何最好地編寫這樣的腳本的單元測試的建議將不勝感激。如何爲ruby編寫rspec測試使用Git的腳本

+0

請問你的代碼Git倉庫互動?您是否正在使用gem,或正在執行'git'命令'exec(「git ...」)'? –

+0

腳本使用ruby-git gem:https://github.com/schacon/ruby-git – FionaCat86

回答

1

我認爲你可以利用的事實,這是完全合法的推到當地的「裸」庫,即您將需要進行如下操作:

  1. 創建一個「裸」倉庫repo1(即一個沒有工作副本的存儲庫,它完全由.git文件夾的內容組成
  2. repo1克隆常規存儲庫repo2
  3. 按照您通常在repo2(添加,提交,推送)中所做的任何更改。檢查推送是否成功。
  4. 檢查數據是否可用repo1

下面是一個示例控制檯會話在我/tmp目錄:

ruby-2.2.0 in /tmp ♥ mkdir repo1 
ruby-2.2.0 in /tmp ♥ cd repo1 
ruby-2.2.0 in /tmp/repo1 ♥ git init --bare 
Initialized empty Git repository in /private/tmp/repo1/ 
ruby-2.2.0 in /tmp/repo1 ♥ cd .. 
ruby-2.2.0 in /tmp ♥ git clone repo1 repo2 
Cloning into 'repo2'... 
warning: You appear to have cloned an empty repository. 
done. 
ruby-2.2.0 in /tmp ♥ cd repo2 
ruby-2.2.0 in /tmp/repo2 on master ♥ git remote -v 
origin /tmp/repo1 (fetch) 
origin /tmp/repo1 (push) 
ruby-2.2.0 in /tmp/repo2 on master ♥ cd .. 
ruby-2.2.0 in /tmp ♥ cd - 
/tmp/repo2 
ruby-2.2.0 in /tmp/repo2 on master ♥ echo 'ohai!' >> readme.txt 
ruby-2.2.0 in /tmp/repo2 on master ♥ git add readme.txt 
ruby-2.2.0 in /tmp/repo2 on master ♥ git commit 
[master (root-commit) 1f1fb96] First commit 
1 file changed, 1 insertion(+) 
create mode 100644 readme.txt 
ruby-2.2.0 in /tmp/repo2 on master ♥ git push 
Counting objects: 3, done. 
Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
To /tmp/repo1 
* [new branch]  master -> master 
ruby-2.2.0 in /tmp/repo2 on master ♥ git log 
commit 1f1fb96deaaf15fa33f1682d87d85014b59b36db 
Author: Alexey Shein <[email protected]> 
Date: Tue Sep 22 01:22:12 2015 +0500 

    First commit 
ruby-2.2.0 in /tmp/repo2 on master ♥ cd .. 
ruby-2.2.0 in /tmp ♥ cd repo1 
ruby-2.2.0 in /tmp/repo1 ♥ ls 
HEAD config description hooks info objects refs 
ruby-2.2.0 in /tmp/repo1 ♥ git log 
commit 1f1fb96deaaf15fa33f1682d87d85014b59b36db 
Author: Alexey Shein <[email protected]> 
Date: Tue Sep 22 01:22:12 2015 +0500 

    First commit 
ruby-2.2.0 in /tmp/repo1 ♥