import tempfile, os

from traitlets.config.loader import Config
import nose.tools as nt


def setup_module():
    ip.magic('load_ext storemagic')

def test_store_restore():
    assert 'bar' not in ip.user_ns, "Error: some other test leaked `bar` in user_ns"
    assert 'foo' not in ip.user_ns, "Error: some other test leaked `foo` in user_ns"
    assert 'foobar' not in ip.user_ns, "Error: some other test leaked `foobar` in user_ns"
    assert 'foobaz' not in ip.user_ns, "Error: some other test leaked `foobaz` in user_ns"
    ip.user_ns['foo'] = 78
    ip.magic('alias bar echo "hello"')
    ip.user_ns['foobar'] = 79
    ip.user_ns['foobaz'] = '80'
    tmpd = tempfile.mkdtemp()
    ip.magic('cd ' + tmpd)
    ip.magic('store foo')
    ip.magic('store bar')
    ip.magic('store foobar foobaz')

    # Check storing
    nt.assert_equal(ip.db['autorestore/foo'], 78)
    nt.assert_in('bar', ip.db['stored_aliases'])
    nt.assert_equal(ip.db['autorestore/foobar'], 79)
    nt.assert_equal(ip.db['autorestore/foobaz'], '80')

    # Remove those items
    ip.user_ns.pop('foo', None)
    ip.user_ns.pop('foobar', None)
    ip.user_ns.pop('foobaz', None)
    ip.alias_manager.undefine_alias('bar')
    ip.magic('cd -')
    ip.user_ns['_dh'][:] = []

    # Check restoring
    ip.magic('store -r foo bar foobar foobaz')
    nt.assert_equal(ip.user_ns['foo'], 78)
    assert ip.alias_manager.is_alias('bar')
    nt.assert_equal(ip.user_ns['foobar'], 79)
    nt.assert_equal(ip.user_ns['foobaz'], '80')

    ip.magic('store -r') # restores _dh too
    nt.assert_in(os.path.realpath(tmpd), ip.user_ns['_dh'])

    os.rmdir(tmpd)

def test_autorestore():
    ip.user_ns['foo'] = 95
    ip.magic('store foo')
    del ip.user_ns['foo']
    c = Config()
    c.StoreMagics.autorestore = False
    orig_config = ip.config
    try:
        ip.config = c
        ip.extension_manager.reload_extension('storemagic')
        nt.assert_not_in('foo', ip.user_ns)
        c.StoreMagics.autorestore = True
        ip.extension_manager.reload_extension('storemagic')
        nt.assert_equal(ip.user_ns['foo'], 95)
    finally:
        ip.config = orig_config

@@ -61,32 +61,8 @@
 fig%0A
-import nose.tools as nt%0A
 %0A%0Ade
@@ -792,39 +792,30 @@
 ing%0A    
-nt.
 assert
-_equal(
+ 
 ip.db%5B
-'
+%22
 autorest
@@ -825,47 +825,44 @@
 /foo
-'%5D,
+%22%5D ==
  78
-)
 %0A    
-nt.
 assert
-_in('bar',
+ %22bar%22 in
  ip.db%5B
-'
+%22
 stor
@@ -875,39 +875,29 @@
 ases
-'%5D)
+%22%5D
 %0A    
-nt.
 assert
-_equal(
+ 
 ip.db%5B
-'
+%22
 auto
@@ -914,43 +914,35 @@
 obar
-'%5D,
+%22%5D ==
  79
-)
 %0A    
-nt.
 assert
-_equal(
+ 
 ip.db%5B
-'
+%22
 auto
@@ -951,33 +951,34 @@
 store/foobaz
-'%5D, '80')
+%22%5D == %2280%22
 %0A%0A    # Remo
@@ -1216,33 +1216,33 @@
 ng%0A    ip.magic(
-'
+%22
 store -r foo bar
@@ -1255,39 +1255,30 @@
 r foobaz
-'
+%22
 )%0A    
-nt.
 assert
-_equal(
+ 
 ip.user_
@@ -1284,19 +1284,20 @@
 _ns%5B
-'foo'%5D,
+%22foo%22%5D ==
  78
-)
 %0A   
@@ -1334,35 +1334,26 @@
 ias(
-'bar'
+%22bar%22
 )%0A    
-nt.
 assert
-_equal(
+ 
 ip.u
@@ -1363,43 +1363,35 @@
 _ns%5B
-'
+%22
 foobar
-'%5D,
+%22%5D ==
  79
-)
 %0A    
-nt.
 assert
-_equal(
+ 
 ip.u
@@ -1401,24 +1401,25 @@
 _ns%5B
-'
+%22
 foobaz
-'%5D, '80')
+%22%5D == %2280%22
 %0A%0A  
@@ -1433,17 +1433,17 @@
 gic(
-'
+%22
 store -r
 ') #
@@ -1438,18 +1438,19 @@
 store -r
-')
+%22) 
  # resto
@@ -1469,21 +1469,15 @@
     
-nt.
 assert
-_in(
+ 
 os.p
@@ -1494,17 +1494,19 @@
 th(tmpd)
-,
+ in
  ip.user
@@ -1513,15 +1513,14 @@
 _ns%5B
-'_dh'%5D)
+%22_dh%22%5D
 %0A%0A  
@@ -1792,33 +1792,33 @@
 eload_extension(
-'
+%22
 storemagic')%0A   
@@ -1803,33 +1803,33 @@
 sion(%22storemagic
-'
+%22
 )%0A        nt.ass
@@ -1826,31 +1826,27 @@
     
-nt.
 assert
-_not_
+ %22foo%22 not 
 in
-('foo',
  ip.
@@ -1852,17 +1852,16 @@
 .user_ns
-)
 %0A       
@@ -1940,17 +1940,17 @@
 tension(
-'
+%22
 storemag
@@ -1951,17 +1951,17 @@
 oremagic
-'
+%22
 )%0A      
@@ -1966,24 +1966,15 @@
     
-nt.
 assert
-_equal(
+ 
 ip.u
@@ -1984,19 +1984,20 @@
 _ns%5B
-'foo'%5D,
+%22foo%22%5D ==
  95
-)
 %0A   

Remove nose