2016-11-07 21 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. 目前,我可以看到一个额外的问题,做承诺的添加/修改文件。贡献者将只能从存储库的基本目录提交。

一旦我解决了问题,我将更新更改。