Repository: ESSS/conda-devenv

Issue:
conda devenv may fail if history file is not there.
Traceback:

```
09:23:35 -> call conda devenv --quiet -n _database10-win64-py36 -f "K:\etk\database10\\environment.devenv.yml" 
09:23:36 Traceback (most recent call last):
09:23:36   File "W:\Miniconda\Scripts\conda-devenv-script.py", line 5, in <module>
09:23:36     exit(main())
09:23:36   File "W:\Miniconda\lib\site-packages\conda_devenv\devenv.py", line 528, in main
09:23:36     truncate_history_file(env_directory)
09:23:36   File "W:\Miniconda\lib\site-packages\conda_devenv\devenv.py", line 354, in truncate_history_file
09:23:36     copyfile(history_filename, history_backup_filename)
09:23:36   File "W:\Miniconda\lib\shutil.py", line 120, in copyfile
09:23:36     with open(src, 'rb') as fsrc:
09:23:36 FileNotFoundError: [Errno 2] No such file or directory: 'W:\\Miniconda\\envs\\_database10-win64-py36\\conda-meta\\history'
09:23:36 Call failed with return code 1
```

Discussion:

Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index b94fed0..24d2588 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -5,11 +5,15 @@ History
 Next
 ------------------
 
-* Obtain  ``envs_dir`` without using a subprocess (`#67`_).
 
+* Do not fail if history file does not exists (`#66`_).
+
+* Obtain  ``envs_dir`` without using a subprocess (`#67`_).
 
+.. _`#66`: https://github.com/ESSS/conda-devenv/issues/66
 .. _`#67`: https://github.com/ESSS/conda-devenv/issues/67
 
+
 1.0.3 (2018-06-20)
 ------------------
 
diff --git a/conda_devenv/devenv.py b/conda_devenv/devenv.py
index ba0586b..a89cf89 100644
--- a/conda_devenv/devenv.py
+++ b/conda_devenv/devenv.py
@@ -345,16 +345,17 @@ def truncate_history_file(env_directory):
     if env_directory is None:
         return  # Environment does not exists, no history to truncate
 
-    from os.path import join
+    from os.path import isfile, join
     from time import time
     from shutil import copyfile
 
     history_filename = join(env_directory, 'conda-meta', 'history')
     history_backup_filename = '%s.%s' % (history_filename, time())
-    copyfile(history_filename, history_backup_filename)
+    if isfile(history_filename):
+        copyfile(history_filename, history_backup_filename)
 
-    with open(history_filename, 'w') as history:
-        history.truncate()
+        with open(history_filename, 'w') as history:
+            history.truncate()
 
 
 def __call_conda_env_update(args, output_filename):


Test patch:
diff --git a/tests/test_truncate_history_file.py b/tests/test_truncate_history_file.py
index 75fc050..190a77f 100644
--- a/tests/test_truncate_history_file.py
+++ b/tests/test_truncate_history_file.py
@@ -21,3 +21,11 @@ def test_truncate_history_file_backups_file(mocker, tmpdir):
     backup = tmpdir.join('conda-meta', 'history.123')
     assert backup.read() == history
     assert history_file.read() == ''
+
+
+def test_truncate_history_file_ingores_missing(mocker, tmpdir):
+    conda_meta_dir = tmpdir.join('conda-meta')
+    conda_meta_dir.ensure(dir=True)  # Just meta folder no history.
+    from conda_devenv.devenv import truncate_history_file
+    truncate_history_file(str(tmpdir))
+    # Truncate should not raise.