2016-11-07 18 views
-1

我使用簡單的測試框架將* .xlsx文件的內容轉換爲硒瀏覽器測試。這允許高層次的抽象,但是由於它是一個二進制文件,所以很討厭在git中工作。是否有辦法強制自動生成的文件與觸發其生成的其餘提交一起提交?

excel的測試文件的格式是:

Column A   -> Action 
Column B   -> Identity (which text field, etc) 
Column C onwards -> Value 

我寫了下面的代碼,成功地轉換Excel文件到一個文本文件(*以.json),本質上只是一個列表的列表(使訂單保持不變,並且不會引入額外的關鍵字)。

#!/usr/bin/env python 

""" 
This script takes a GUI test in xlsx format and outputs a text interpretation. 
""" 

import argparse 
import json 
from openpyxl import load_workbook 
from openpyxl.cell import get_column_letter 


def main(xlsx_test): 
    ws = load_workbook(filename=xlsx_test, data_only=True).active 
    col_size = len(ws.column_dimensions) 
    row_size = len(ws.row_dimensions) 
    full_document = [] 

    for col in xrange(2, col_size): 
     col_letter = get_column_letter(col + 1) 
     test_document = [] 

     for row in xrange(row_size): 
      cell_reference = col_letter + str(row + 1) 
      cell_value = ws[cell_reference].value 

      if cell_value: 
       action = ws['A' + str(row + 1)].value 
       identity = ws['B' + str(row + 1)].value 

       if not action: 
        action = "" 
       if not identity: 
        identity = "" 

       test_item = [action, identity, cell_value] 
       test_document.append(test_item) 
     if test_document: 
      full_document.append(test_document) 

    with open(xlsx_test + '.json', 'w') as outfile: 
     json.dump(full_document, outfile, indent=4) 


if __name__ == '__main__': 

    parser = argparse.ArgumentParser(description='Generate text verion of tests') 
    test_file = parser.add_argument(
     '--input', 
     type=str, 
     nargs='?', 
     help='the file to generate text version of tests from' 
    ) 

    args = parser.parse_args() 

    if args.input is None: 
     print '''No file selected''' 
    else: 
     main(args.input) 

要運行它,我將執行在bash腳本如下:

python /path/to/this/script.py --input='/path/to/test.xlsx' 

我想的文本文件,其產生與XLSX二進制只是讓我們得到了一個一起被提交git改變歷史。

我一直在閱讀預先提交的鉤子(我很樂意在每個貢獻者的機器上設置,但我很努力確定它是否將一個額外的生成文件包含到提交中。將是偉大的,因爲我不知道該怎麼試試此刻

我還必須弄清楚我是否可以遍歷提交文件列表中的擴展名爲* .xlsx,並將路徑作爲上面的python腳本的參數,在一個循環中,我問的是這樣做的嗎?

回答

0

我制定了一個令人滿意的解決方案。我創建了兩個提交鉤子。

一個pre-commit鉤子:

#!/bin/bash 
echo 
touch .commit 
exit 

和後commit鉤子:

#!/bin/bash 
if [ -e .commit ] 
    then 
    rm .commit 
    git diff-tree --no-commit-id --name-only -r HEAD | while read i; do 
     if [[ "${i}" == *.xlsx ]]; then 
      echo 
      echo ➤ Found an Excel file: $(basename "${i}") in the git diff-tree 
      if [ ! -f "${i}" ]; then 
       echo ➤ $(basename "${i}") was deleted. Not generating a text interpretation 
       if [ -f "${i}.json" ]; then 
        echo ➤ Found an orphaned text interpretation: $(basename "${i}").json 
        rm "${i}.json" 
        echo ➤ Removed $(basename "${i}").json 
        git rm -q "${i}.json" 
        echo ➤ Removed reference to $(basename "${i}").json from git 
       fi 
      else 
       python "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../../relative/path/to/script.py" --input="$(pwd)/${i}" 
       echo ➤ Generated text interpretation: $(basename "${i}").json 
       git add "${i}.json" 
       echo ➤ Added $(basename "${i}").json to git 
      fi 
     fi 
    done 
    echo ----------------------------------------------------------------------- 
    git commit --amend -C HEAD --no-verify 
    echo ➤ Ammended commit above ⬏ 
    echo ----------------------------------------------------------------------- 
    echo ➤ Initial commit below ⬎ 
fi 
exit 
  1. 目前,我可以看到一個額外的問題,做承諾的添加/修改文件。貢獻者將只能從存儲庫的基本目錄提交。

一旦我解決了問題,我將更新更改。