#!/usr/bin/env python
from __future__ import print_function
"""
Test check_if_uptodate

    Bug: @parallel sets @check_if_uptodate to None.
    Submitted by Jafar Taghiyar (jafar<dot>taghiyar<at>gmail.com)
    https://github.com/bunbun/ruffus/issues/53

"""
import os


import os
tempdir = os.path.abspath(os.path.abspath(os.path.splitext(__file__)[0]))
exe_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
import sys

# add grandparent to search path for testing
grandparent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
sys.path.insert(0, grandparent_dir)

# module name = script name without extension
module_name = os.path.splitext(os.path.basename(__file__))[0]


from ruffus import posttask, parallel, check_if_uptodate, follows, touch_file, pipeline_printout, pipeline_run



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   imports


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

import unittest
import shutil
try:
    from StringIO import StringIO
except:
    from io import StringIO
from collections import defaultdict

# use simplejson in place of json for python < 2.6
import json
#try:
#    import json
#except ImportError:
#    import simplejson
#    json = simplejson



def sentinel_file_exists(output_file):
    if not os.path.exists(output_file):
        return True, "Missing file %s" % output_file
    else:
        return False, "File %s exists" % output_file
        
@posttask(touch_file(os.path.join(tempdir, "task1_completed.flag")))
@parallel([[os.path.join(tempdir, "task1_completed.flag")]])
@check_if_uptodate(sentinel_file_exists)
def task1(x):
    pass

@follows(task1)    
@posttask(touch_file(os.path.join(tempdir, "task2_completed.flag")))
@parallel([[os.path.join(tempdir, "task2_completed.flag")]])
@check_if_uptodate(sentinel_file_exists)
def task2(x):
    pass
    







#
class Test_ruffus(unittest.TestCase):
    def setUp(self):
        try:
            shutil.rmtree(tempdir)
        except:
            pass
        os.makedirs(tempdir)

    def tearDown(self):
        try:
            shutil.rmtree(tempdir)
        except:
            pass

    def test_ruffus (self):
        # run first
        pipeline_run(verbose = 0)
        # should now be out of date
        s = StringIO()
        pipeline_printout(s, verbose = 5)

        ret = s.getvalue()
        try:
            self.assertNotRegexpMatches(ret, r"Jobs needs update:\s*No function to check if up-to-date")
        except:
            print ("Oops: @check_if_uptodate is not being picked up\n")
            raise
        self.assertRegexpMatches(ret, r"Tasks which are up-to-date:(\n\s*)*Task = 'test_check_if_uptodate.task1'(\n\s*)*Task = 'test_check_if_uptodate.task2'")


if __name__ == '__main__':
    unittest.main()


@@ -1133,198 +1133,8 @@
 gIO%0A
-from collections import defaultdict%0A%0A# use simplejson in place of json for python %3C 2.6%0Aimport json%0A#try:%0A#    import json%0A#except ImportError:%0A#    import simplejson%0A#    json = simplejson%0A
 %0A%0A%0Ad
@@ -1758,13 +1758,8 @@
 %0A%0A%0A%0A
-%0A%0A%0A#%0A
 clas
@@ -1788,24 +1788,495 @@
 .TestCase):%0A
+%0A    def do_assertNotRegexpMatches (self, test_str, re_str):%0A        import re%0A        if re.search(re_str, test_str) is not None:%0A            raise AssertionError(%22Regexp matched: %25r found in %25r%22 %25 (re_str, test_str))%0A%0A    def do_assertRegexpMatches (self, test_str, re_str):%0A        import re%0A        if re.search(re_str, test_str) is None:%0A            #AssertionError: %0A            raise AssertionError(%22Regexp didn't match: %25r not found in %25r%22 %25 (re_str, test_str))%0A%0A
     def setU
@@ -2742,25 +2742,25 @@
    self.
+do_
 assert
-Not
 RegexpMa
@@ -2776,63 +2776,125 @@
 , r%22
-Jobs needs update:%5Cs*No function to 
+Tasks which are up-to-date:(%5Cn%5Cs*)*Task = 'test_check_if_uptodate.task1'(%5Cn%5Cs*)*Task = 'test_
 check
- if 
+_if_
 up
--
 to
--
 date
+.task2'
 %22)%0A 
@@ -2932,55 +2932,53 @@
 t (%22
+%5Cn%5Ct
 Oops: 
-@check_if_uptodate is not being picked up
+Both tasks should be up to date!!%5Cn
 %5Cn%22)
@@ -2996,16 +2996,33 @@
   raise%0A
+        try:%0A    
         
@@ -3026,22 +3026,28 @@
    self.
+do_
 assert
+Not
 RegexpMa
@@ -3063,127 +3063,179 @@
 , r%22
-Tasks which are up-to-date:(%5Cn%5Cs*)*Task = 'test_
+Jobs needs update:%5Cs*No function to 
 check
-_if_
+ if 
 up
+-
 to
+-
 date
-.task1'(%5Cn%5Cs*)*Task = 'test_check_if_uptodate.task2'%22)
+%22)%0A        except:%0A            print (%22%5Cn%5CtOops: @check_if_uptodate is not being picked up!!%5Cn%5Cn%22)%0A            raise
 %0A%0A%0Ai
@@ -3281,8 +3281,9 @@
 main()%0A%0A
+%0A

Work around python 2.6 unittest.TestCase doesn't have assertNotRegexpMatches