From cb17765453e69391b901f99b385400abe5726c0b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 31 Dec 2013 23:38:25 +0000 Subject: [PATCH 1/4] Check that serialized data is stable. Extend the filemanip tests to ensure that the data is serialized the same way every time. Signed-off-by: brian m. carlson --- test/testfilemanip.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/testfilemanip.py b/test/testfilemanip.py index 8d5f104..14b5683 100755 --- a/test/testfilemanip.py +++ b/test/testfilemanip.py @@ -22,12 +22,23 @@ class TestRoundTripping(unittest.TestCase): else: self.assertEqual(a.uuid, b.uuid) self.assertEqual(a.table, b.table) + def check_serialized_data(self, fs, filename, recs): + fp = open(filename, "rb") + data = fp.read() + fp.close() + fs.store(recs) + fp = open(filename, "rb") + data2 = fp.read() + fp.close() + self.assertEqual(data, data2) def run_test(self, fmt, lossy=False): options = {"table-is-field-0": True} - fs = FileStorage(fmt, self.tempdir.name + "/" + fmt, options=options) + filename = self.tempdir.name + "/" + fmt + fs = FileStorage(fmt, filename, options=options) fs.store(self.data) m = fs.load() self.compare(self.data, m, lossy) + self.check_serialized_data(fs, filename, m) def test_csv(self): self.run_test("csv", True) def test_json(self): From 0d14ec597b330b1f7cbbe71566ebaf3b5a365d07 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 31 Dec 2013 23:41:28 +0000 Subject: [PATCH 2/4] filemanip: add tests for YAML. Since currently we can only serialize YAML, not load it, verify that it serializes correctly twice. Signed-off-by: brian m. carlson --- test/testfilemanip.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testfilemanip.py b/test/testfilemanip.py index 14b5683..e7a83ea 100755 --- a/test/testfilemanip.py +++ b/test/testfilemanip.py @@ -43,6 +43,13 @@ class TestRoundTripping(unittest.TestCase): self.run_test("csv", True) def test_json(self): self.run_test("json") + def test_yaml(self): + options = {"table-is-field-0": True} + fmt = "yaml" + filename = self.tempdir.name + "/" + fmt + fs = FileStorage(fmt, filename, options=options) + fs.store(self.data) + self.check_serialized_data(fs, filename, self.data) def test_pickle(self): self.run_test("pickle") From a83ae0f348d6bbe9131ca7d631099573c8ba1e01 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Wed, 1 Jan 2014 20:02:21 +0000 Subject: [PATCH 3/4] database: add some tests for git transactions. Signed-off-by: brian m. carlson --- test/testdatabase.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/testdatabase.py b/test/testdatabase.py index f2c6b4a..877f03c 100755 --- a/test/testdatabase.py +++ b/test/testdatabase.py @@ -2,6 +2,8 @@ from newfol.database import DatabaseVersion, Database, Schema from newfol.filemanip import Record +import os.path +import tempfile import unittest class TestDatabaseVersion(unittest.TestCase): @@ -41,5 +43,16 @@ class TestDatabaseAccessors(unittest.TestCase): self.assertEqual(obj.serialization(), DatabaseVersion.preferred().serialization()) +class TestGitTransactions(unittest.TestCase): + def setUp(self): + self.ddir = tempfile.TemporaryDirectory() + fp = open(self.ddir.name + "/schema", "w") + fp.write("fmt:0:newfol schema file:\ntxn:git\n") + fp.close() + self.db = Database.load(self.ddir.name) + def test_has_git_dir(self): + self.db.store() + self.assertTrue(os.path.isdir(self.ddir.name + "/.git")) + if __name__ == '__main__': unittest.main() From 544fc064e998ca06cc9d111093a53e5207c744c6 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Wed, 1 Jan 2014 20:30:03 +0000 Subject: [PATCH 4/4] filemanip: use reasonable committer ID for git. We should not rely on the user setting up reasonable values for the committer identification for git. Use a committer name of newfol and an email address consisting of the user's username and the FQDN of the machine. Signed-off-by: brian m. carlson --- lib/newfol/filemanip.py | 19 ++++++++++++++++--- test/testdatabase.py | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/newfol/filemanip.py b/lib/newfol/filemanip.py index eeeb869..05746ed 100644 --- a/lib/newfol/filemanip.py +++ b/lib/newfol/filemanip.py @@ -7,7 +7,9 @@ import json import os import os.path import pickle +import pwd import re +import socket import subprocess import sys import uuid @@ -113,10 +115,21 @@ class GitTransactionStore(TransactionStore): def _call_git(*args): with open(os.devnull, "r") as devnullr: with open(os.devnull, "w+") as devnullw: - cmdline = ["git"] + env = dict(os.environ) + for i in ('NAME', 'EMAIL', 'DATE'): + for j in ('AUTHOR', 'COMMITTER'): + try: + del env['GIT_%s_%s' % (j, i)] + except KeyError: + pass + user = pwd.getpwuid(os.getuid())[0] + fqdn = socket.getfqdn() + cmdline = ["git", "-c", "user.name=newfol", + "-c", "user.email=" + user + "@" + fqdn] cmdline.extend(args) - return subprocess.call(cmdline, stdin=devnullr, stdout=devnullw, - stderr=devnullw) + p = subprocess.Popen(cmdline, stdin=devnullr, stdout=devnullw, + stderr=devnullw, env=env) + return p.wait() def prepare_open(self, filename, mode): (self._prefix, self._name) = os.path.split(filename) self._mode = mode diff --git a/test/testdatabase.py b/test/testdatabase.py index 877f03c..b834125 100755 --- a/test/testdatabase.py +++ b/test/testdatabase.py @@ -2,7 +2,10 @@ from newfol.database import DatabaseVersion, Database, Schema from newfol.filemanip import Record +import os import os.path +import pwd +import socket import tempfile import unittest @@ -53,6 +56,25 @@ class TestGitTransactions(unittest.TestCase): def test_has_git_dir(self): self.db.store() self.assertTrue(os.path.isdir(self.ddir.name + "/.git")) + def get_format_data(self, fmt): + cwd = os.getcwd() + data = None + try: + os.chdir(self.ddir.name) + fp = os.popen("git log --pretty=format:" + fmt + " -n1") + data = fp.read() + fp.close() + finally: + os.chdir(cwd) + return data + def test_correct_committer_name(self): + self.db.store() + self.assertEqual(self.get_format_data("%cn"), "newfol") + def test_correct_committer_email(self): + self.db.store() + user = pwd.getpwuid(os.getuid())[0] + fqdn = socket.getfqdn() + self.assertEqual(self.get_format_data("%ce"), user + "@" + fqdn) if __name__ == '__main__': unittest.main()