import datetime

import os

import pytest

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.mark.parametrize("name", [
    "lorem",
    "ipsum",
])
@pytest.mark.parametrize("dir", [
    ".vimrc",
])
def test_backup_dirs(host, name, dir):
    t = datetime.datetime.today().isoformat()[:10]
    c = "find /home/{0} -name {1}.{2}* | sort -r | head -n1"
    b = host.run(c.format(name, dir, t))
    d = host.file(b.stdout)

    assert b.rc == 0
    assert d.exists
    assert d.user == name
    assert d.group == name


@pytest.mark.parametrize("name", [
    "lorem",
    "ipsum",
])
def test_janus_install(host, name):
    d = host.file("/home/{0}/.vim/janus/vim/".format(name))

    assert d.exists
    assert d.user == name
    assert d.group == name


@pytest.mark.parametrize("name", [
    "lorem",
    "ipsum",
])
@pytest.mark.parametrize("plugin", [
    "lightline.vim",
    "vim-surround",
])
def test_plugin_install(host, name, plugin):
    d = host.file("/home/{0}/.janus/{1}".format(name, plugin))

    assert d.exists
    assert d.user == name
    assert d.group == name

@@ -1,21 +1,4 @@
-import datetime%0A%0A
 impo
@@ -274,19 +274,20 @@
 etrize(%22
-dir
+file
 %22, %5B%0A   
@@ -316,19 +316,20 @@
 _backup_
-dir
+file
 s(host, 
@@ -338,289 +338,120 @@
 me, 
-dir
+file
 ):%0A    
-t = datetime.datetime.today().isoformat()%5B:10%5D%0A    c = %22find /home/%7B0%7D -name %7B1%7D.%7B2%7D* %7C sort -r %7C head -n1%22%0A    b = host.run(c.format(name, dir, t))%0A    d = host.file(b.stdout)%0A%0A    assert b.rc == 0%0A    assert d.exists%0A    assert d.user == name%0A    assert d.group == name
+n = host.run(%22find . -type f -name '%7B%7D.*' %7C wc -l%22.format(file))%0A%0A    assert int(float(n.stdout)) %3E 0
 %0A%0A%0A@

Simplify backup-file test (and rename)