@ -2,13 +2,20 @@ import json
import pathlib
import pytest
from transpose import Transpose , version , DEFAULT_CACHE_FILENAME
from transpose import Transpose
from transpose.exceptions import TransposeError
from .utils import STORE_DIR , TARGET_DIR , setup_env
from .utils import (
STORE_DIR ,
STORED_DIR ,
TARGET_DIR ,
setup_restore ,
setup_store ,
setup_apply ,
)
@setup_env ( )
@setup_stor e ( )
def test_init ( ) :
t = Transpose (
target_path = TARGET_DIR ,
@ -24,12 +31,10 @@ def test_init():
assert t . cache_path == pathlib . Path ( TARGET_DIR ) . joinpath ( " .transpose.txt " )
@setup_env ( )
@setup_apply ( )
def test_apply ( ) :
store_path = pathlib . Path ( STORE_DIR )
target_path = pathlib . Path ( TARGET_DIR )
store_path . rmdir ( )
target_path . rename ( store_path )
t = Transpose (
target_path = STORE_DIR ,
@ -56,30 +61,34 @@ def test_apply():
assert target_path . is_dir ( ) and target_path . is_symlink ( )
@setup_env ( )
def test_store_restore ( ) :
@setup_stor e ( )
def test_store ( ) :
t = Transpose (
target_path = TARGET_DIR ,
store_path = STORE_DIR ,
)
t . store ( " TestStore " )
target_path = pathlib . Path ( TARGET_DIR )
store_path = pathlib . Path ( STORE_DIR ) . joinpath ( " TestStore " )
t . store ( " TestStore " )
# STORE
## Successful Store
assert store_path . is_dir ( ) and not store_path . is_symlink ( )
assert target_path . is_dir ( ) and target_path . is_symlink ( )
assert t . cache_path . is_file ( )
@setup_restore ( )
def test_restore ( ) :
target_path = pathlib . Path ( TARGET_DIR )
stored_path = pathlib . Path ( STORE_DIR ) . joinpath ( STORED_DIR )
t = Transpose (
target_path = str ( store_path ) ,
target_path = str ( stored _path ) ,
store_path = STORE_DIR ,
)
# RESTORE
## Missing Cache File
# Missing Cache File
cache = t . cache_path . read_text ( )
t . cache_path . unlink ( )
with pytest . raises ( TransposeError ) :
@ -87,15 +96,20 @@ def test_store_restore():
t . cache_path . write_text ( cache )
cache = json . loads ( cache )
## Missing Target Path
# Missing Target Path (original path)
t . target_path . rename ( " newpath " )
with pytest . raises ( TransposeError ) :
t . restore ( )
pathlib . Path ( " newpath " ) . rename ( t . target_path )
## Successful Restore
# Original Path is a symlink - Should be removed and successfully restore
original_path = pathlib . Path ( cache [ " original_path " ] )
original_path . rename ( " newpath " )
original_path . symlink_to ( " newpath " )
# Successful
t . restore ( )
assert not store_path . exists ( )
assert target_path . is_dir ( ) and not target_path . is_symlink ( )
assert not stored_path . exists ( )
assert not t . cache_path . exists ( )