Exemple #1
0
def test_list(book):
    # success: defaults
    assert out(book, list) == [
        'alpha\n',
        'bravo\n',
        'charlie\n',
    ]

    # success: GLOB
    assert out(book, list, 'a*') == [
        'alpha\n',
    ]

    # success: --reverse --sort size
    assert out(book, list, '-r', '-s', 'size') == [
        'charlie\n',
        'bravo\n',
        'alpha\n',
    ]

    # success: --sort time
    book['bravo'].time = time.localtime(time.time() + 1)
    book['charlie'].time = time.localtime(time.time() + 2)
    book['alpha'].time = time.localtime(time.time() + 3)
    assert out(book, list, '-s', 'time') == [
        'bravo\n',
        'charlie\n',
        'alpha\n',
    ]
Exemple #2
0
def test_find(book):
    # success: defaults
    assert out(book, find, 'alpha') == [
        'alpha\n',
    ]

    # success: --regex
    assert out(book, find, r'a\w{3}a', '-r') == [
        'alpha\n',
    ]
Exemple #3
0
def test_copy(book):
    # setup
    dest = book['alpha'].path.replace('alpha', 'dest')

    # success: defaults
    assert out(book, copy, 'alpha', 'dest') == []
    assert os.path.exists(dest)
    assert os.path.exists(book['alpha'].path)

    # failure: dest exists
    assert out(book, copy, 'bravo', 'charlie') == [
        "Error: Note 'charlie' already exists.\n",
    ]
Exemple #4
0
def test_wget(book):
    # success: defaults
    assert out(book, wget, 'alpha', 'example.com') == []
    assert '<title>Example Domain</title>' in book['alpha'].read()

    # failure: invalid address
    assert out(book, wget, 'alpha', 'nope.nope') == [
        "Error: Could not find 'http://nope.nope'.\n",
    ]

    # failure: nonexistent address
    num = random.randrange(1000000, 2000000)
    assert out(book, wget, 'alpha', f'nope{num}.com') == [
        f"Error: Could not find 'http://nope{num}.com'.\n",
    ]
Exemple #5
0
def test_dump(book, tmpdir):
    # setup
    dest = tmpdir.join('test.zip')

    # success: defaults
    assert out(book, dump, dest) == []
    with zipfile.ZipFile(dest, 'r') as zipf:
        assert zipf.namelist() == ['alpha.txt', 'bravo.txt', 'charlie.txt']
        assert zipf.open('alpha.txt').read() == b'alpha'
        assert zipf.open('bravo.txt').read() == b'bravobravo'
        assert zipf.open('charlie.txt').read() == b'charliecharliecharlie'

    # success: --level 0
    assert out(book, dump, dest, '-l', '0') == []
    with zipfile.ZipFile(dest, 'r') as zipf:
        assert os.stat(dest).st_size == 359
Exemple #6
0
def test_show(book):
    # setup
    book['bravo'].write('test ' * 25)

    # success: defaults
    assert out(book, show, 'alpha') == [
        'alpha\n',
    ]

    # success: --wrap 40
    assert out(book, show, 'bravo', '-w', 40) == [
        'test test test test test test test test\n',
        'test test test test test test test test\n',
        'test test test test test test test test\n',
        'test\n',
    ]
Exemple #7
0
def test_copy(book, monkeypatch):
    # setup
    args = []
    monkeypatch.setattr(send2trash, 'send2trash', lambda *a: args.extend(a))

    # success: defaults
    assert out(book, drop, 'alpha') == []
    assert args == [book['alpha'].path]
Exemple #8
0
def test_list(book):
    # success: defaults
    assert out(book, list) == [
        'alpha\n',
        'bravo\n',
        'charlie\n',
    ]

    # success: GLOB
    assert out(book, list, 'a*') == [
        'alpha\n',
    ]

    # success: --reverse --sort size
    assert out(book, list, '-r', '-s', 'size') == [
        'charlie\n',
        'bravo\n',
        'alpha\n',
    ]
Exemple #9
0
def test_edit(book, monkeypatch):
    # setup
    args = {}
    func = lambda a, **k: args.update({'args': a, **k})
    monkeypatch.setattr(subprocess, 'run', func)

    # success: defaults
    assert out(book, edit, 'alpha') == []
    assert args == {
        'args': [book['alpha'].path],
        'shell': True,
    }

    # success: --editor
    assert out(book, edit, 'alpha', '-e', 'test') == []
    assert args == {
        'args': ['test', book['alpha'].path],
        'shell': True,
    }
Exemple #10
0
def test_clip(book):
    # setup (to preserve pre-test clipboard)
    pre = pyperclip.paste()

    # success: defaults
    assert out(book, clip, 'alpha') == []
    assert pyperclip.paste() == 'alpha'

    # teardown
    pyperclip.copy(pre)
Exemple #11
0
def test_edit(book, monkeypatch):
    # setup
    editargs = {}
    monkeypatch.setattr(click, 'edit', lambda **k: editargs.update(k))

    # success: defaults
    assert out(book, edit, 'alpha') == []
    assert editargs == {
        'editor':    None,
        'extension': '.txt',
        'filename':  book['alpha'].path,
    }

    # success: --editor
    assert out(book, edit, 'alpha', '-e', 'test') == []
    assert editargs == {
        'editor':    'test',
        'extension': '.txt',
        'filename':  book['alpha'].path,
    }
Exemple #12
0
def test_repl(book, monkeypatch):
    # setup
    args = {}
    monkeypatch.setattr(code, 'interact', lambda **k: args.update(k))

    # success: defaults
    assert out(book, repl) == []
    assert args == {
        'banner':  VERSION_STRING,
        'local':   {'book': book, 'group': group},
        'exitmsg': '',
    }
Exemple #13
0
def test_make(book, tmpdir):
    # setup
    file = tmpdir.join('test.txt')
    file.write('test')

    # success: default
    assert out(book, make, 'make') == []
    assert 'make' in book
    assert book['make'].path.endswith('make.txt')
    assert book['make'].read() == ''

    # success: --file FILE
    assert out(book, make, 'make2', '-f', file) == []
    assert book['make2'].read() == 'test'

    # failure: note already exists
    assert out(book, make, 'alpha') == [
        "Error: Note 'alpha' already exists.\n",
    ]

    # failure: cannot read file
    assert out(book, make, 'make3', '-f', 'nope.txt') == [
        "Error: Cannot read file 'nope.txt'.\n",
    ]
Exemple #14
0
def test_group(book):
    # success
    assert out(book, mock, 'test') == [f'{book.dire}\n', 'test\n']

    # success: version callback
    assert out(book, mock, '-v') == [f'{VERSION_STRING}\n']