# Path: tests/conftest.py
# def call_cmd(cmd, expected_exit_code=0):
#     proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode('utf-8').strip()
#     stderr = stderr.decode('utf-8').strip()
#     if proc.returncode != expected_exit_code:
#         print(stdout)
#         print(stderr)
#         assert proc.returncode == expected_exit_code
#     return (stdout, stderr)
# 
# NASA_EXPECTED_FILES = {
#     'globe_west_540.jpg',
#     'globe_west_540_thumb.jpg',
#     'nasa_archive.torrent',
#     'nasa_meta.sqlite',
#     'nasa_files.xml',
#     'nasa_meta.xml',
#     'nasa_reviews.xml',
#     'nasa_itemimage.jpg',
#     'globe_west_540_thumb.jpg',
#     '__ia_thumb.jpg',
# }
# 
# def files_downloaded(path):
#     found_files = set()
#     try:
#         found_files = set(os.listdir(path))
#     except (FileNotFoundError, WindowsError, OSError):
#         pass
#     return found_files


import sys
import os
import time
from tests.conftest import call_cmd, NASA_EXPECTED_FILES, files_downloaded

def test_glob(tmpdir_ch):
    expected_files = {
        'globe_west_540.jpg',
        'globe_west_540_thumb.jpg',
        'nasa_itemimage.jpg',
        '__ia_thumb.jpg',
    }

    call_cmd('ia --insecure download --glob="*jpg" nasa')
    assert files_downloaded(path='nasa') == expected_files


def test_format(tmpdir_ch):
    call_cmd('ia --insecure download --format="Archive BitTorrent" nasa')
    assert files_downloaded(path='nasa') == {'nasa_archive.torrent'}


def test_on_the_fly_format():
    i = 'wonderfulwizardo00baumiala'

    stdout, stderr = call_cmd(f'ia --insecure download --dry-run --format="DAISY" {i}')
    assert stdout == ''

    stdout, stderr = call_cmd(f'ia --insecure download --dry-run --format="DAISY" --on-the-fly {i}')
    assert stdout == f'http://archive.org/download/{i}/{i}_daisy.zip'


def test_clobber(tmpdir_ch):
    cmd = 'ia --insecure download nasa nasa_meta.xml'

# Path: tests/conftest.py
# def call_cmd(cmd, expected_exit_code=0):
#     proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode('utf-8').strip()
#     stderr = stderr.decode('utf-8').strip()
#     if proc.returncode != expected_exit_code:
#         print(stdout)
#         print(stderr)
#         assert proc.returncode == expected_exit_code
#     return (stdout, stderr)
# 
# NASA_EXPECTED_FILES = {
#     'globe_west_540.jpg',
#     'globe_west_540_thumb.jpg',
#     'nasa_archive.torrent',
#     'nasa_meta.sqlite',
#     'nasa_files.xml',
#     'nasa_meta.xml',
#     'nasa_reviews.xml',
#     'nasa_itemimage.jpg',
#     'globe_west_540_thumb.jpg',
#     '__ia_thumb.jpg',
# }
# 
# def files_downloaded(path):
#     found_files = set()
#     try:
#         found_files = set(os.listdir(path))
#     except (FileNotFoundError, WindowsError, OSError):
#         pass
#     return found_files


# Path: tests/cli/test_ia_download.py
import sys
import os
import time
from tests.conftest import call_cmd, NASA_EXPECTED_FILES, files_downloaded


def test_glob(tmpdir_ch):
    expected_files = {
        'globe_west_540.jpg',
        'globe_west_540_thumb.jpg',
        'nasa_itemimage.jpg',
        '__ia_thumb.jpg',
    }

    call_cmd('ia --insecure download --glob="*jpg" nasa')
    assert files_downloaded(path='nasa') == expected_files


def test_format(tmpdir_ch):
    call_cmd('ia --insecure download --format="Archive BitTorrent" nasa')
    assert files_downloaded(path='nasa') == {'nasa_archive.torrent'}


def test_on_the_fly_format():
    i = 'wonderfulwizardo00baumiala'

    stdout, stderr = call_cmd(f'ia --insecure download --dry-run --format="DAISY" {i}')
    assert stdout == ''

    stdout, stderr = call_cmd(f'ia --insecure download --dry-run --format="DAISY" --on-the-fly {i}')
    assert stdout == f'http://archive.org/download/{i}/{i}_daisy.zip'


def test_clobber(tmpdir_ch):
    cmd = 'ia --insecure download nasa nasa_meta.xml'

    call_cmd(cmd)