Repository: radiasoft/pykern

Issue:
pkunit.file_eq always should compare files
Almost always compares objects instead

Discussion:

Patch:
diff --git a/pykern/pkunit.py b/pykern/pkunit.py
index c76ac2a..fc72b1b 100644
--- a/pykern/pkunit.py
+++ b/pykern/pkunit.py
@@ -239,12 +239,17 @@ def file_eq(expect_path, *args, **kwargs):
         actual_path = b
     if not isinstance(actual_path, pykern.pkconst.PY_PATH_LOCAL_TYPE):
         actual_path = work_dir().join(actual_path)
-    actual = kwargs['actual'] if a else pkio.read_text(actual_path)
+    if a:
+        actual = kwargs['actual']
+        if actual_path.exists():
+            pkfail('actual={} and actual_path={} both exist', actual, actual_path)
+    else:
+        actual = pkio.read_text(actual_path)
     if expect_path.ext == '.json' and not actual_path.exists():
-        e = pykern.pkjson.load_any(expect_path)
+        e = pkio.read_text(expect_path)
         if a:
             pkio.mkdir_parent_only(actual_path)
-            pykern.pkjson.dump_pretty(actual, filename=actual_path)
+            actual = pykern.pkjson.dump_pretty(actual, filename=actual_path)
     else:
         if j:
             import pykern.pkjinja


Test patch:
diff --git a/tests/pkunit_data/file_eq1.json b/tests/pkunit_data/file_eq1.json
new file mode 100644
index 0000000..7326da5
--- /dev/null
+++ b/tests/pkunit_data/file_eq1.json
@@ -0,0 +1,1 @@
+"array('d', [1.0])"
diff --git a/tests/pkunit_data/file_eq2.txt b/tests/pkunit_data/file_eq2.txt
new file mode 100644
index 0000000..e69de29
diff --git a/tests/pkunit_data/file_eq3.txt b/tests/pkunit_data/file_eq3.txt
new file mode 100644
index 0000000..339f0be
--- /dev/null
+++ b/tests/pkunit_data/file_eq3.txt
@@ -0,0 +1,1 @@
+something else
\ No newline at end of file
diff --git a/tests/pkunit_test.py b/tests/pkunit_test.py
index 31acebb..40b0b6e 100644
--- a/tests/pkunit_test.py
+++ b/tests/pkunit_test.py
@@ -5,6 +5,9 @@ u"""PyTest for :mod:`pykern.pkunit`
 :license: http://www.apache.org/licenses/LICENSE-2.0.html
 """
 from __future__ import absolute_import, division, print_function
+import pkgutil
+
+import py
 import pytest
 
 def test_assert_object_with_json():
@@ -55,6 +58,22 @@ def test_empty_work_dir():
         'Ensure directory was created'
 
 
+def test_file_eq():
+    import array
+    import pykern.pkunit
+    import pykern.pkio
+
+    a = array.ArrayType('d', [1])
+    pykern.pkunit.file_eq('file_eq1.json', actual=a)
+
+    with pykern.pkunit.pkexcept(TypeError):
+        pykern.pkunit.file_eq('file_eq2.txt', actual=dict())
+    d = pykern.pkunit.empty_work_dir()
+    pykern.pkio.write_text(d.join('file_eq3.txt'), 'something')
+    with pykern.pkunit.pkexcept('both exist'):
+        pykern.pkunit.file_eq('file_eq3.txt', actual='something else')
+
+
 def test_import_module_from_data_dir(monkeypatch):
     from pykern import pkunit