# Licensed under a 3-clause BSD style license - see LICENSE.rst

import glob
import os
import sys
import json
from os.path import join, isfile

import pytest

from asv import util

from . import tools


def test_run_publish(capfd, basic_conf_2):
    tmpdir, local, conf, machine_file = basic_conf_2
    tmpdir = util.long_path(tmpdir)

    conf.matrix = {
        "req": dict(conf.matrix),
        "env": {"SOME_TEST_VAR": ["1"]},
    }

    # Tests a typical complete run/publish workflow
    ret = tools.run_asv_with_conf(conf, 'run', "master", '--steps=2',
                                  '--quick', '--show-stderr', '--profile',
                                  '-a', 'warmup_time=0',
                                  '--durations=5',
                                  _machine_file=machine_file)
    assert ret is None
    text, err = capfd.readouterr()

    assert len(os.listdir(join(tmpdir, 'results_workflow', 'orangutan'))) == 5
    assert len(os.listdir(join(tmpdir, 'results_workflow'))) == 2
    assert 'asv: benchmark timed out (timeout 0.1s)' in text
    assert 'total duration' in text

    tools.run_asv_with_conf(conf, 'publish')

    assert isfile(join(tmpdir, 'html', 'index.html'))
    assert isfile(join(tmpdir, 'html', 'index.json'))
    assert isfile(join(tmpdir, 'html', 'asv.js'))
    assert isfile(join(tmpdir, 'html', 'asv.css'))

    # Check parameterized test json data format
    filename = glob.glob(join(tmpdir, 'html', 'graphs', 'arch-x86_64',
                              'asv_dummy_test_package_1',
                              'asv_dummy_test_package_2-' + tools.DUMMY2_VERSIONS[1],
                              'branch-master',
                              'cpu-Blazingly fast',
                              'env-SOME_TEST_VAR-1',
                              'machine-orangutan',
                              'os-GNU_Linux', 'python-*', 'ram-128GB',
                              'params_examples.time_skip.json'))[0]
    with open(filename, 'r') as fp:
        data = json.load(fp)
        assert len(data) == 2
        assert isinstance(data[0][0], int)  # revision
        assert len(data[0][1]) == 3
        assert len(data[1][1]) == 3
        assert isinstance(data[0][1][0], float)
        assert isinstance(data[0][1][1], float)
        assert data[0][1][2] is None

    # Check that the skip options work
    capfd.readouterr()
    tools.run_asv_with_conf(conf, 'run', "master", '--steps=2',
                            '--quick', '--skip-existing-successful',
                            '--bench=time_secondary.track_value',
                            '--skip-existing-failed',
                            _machine_file=join(tmpdir, 'asv-machine.json'))
    tools.run_asv_with_conf(conf, 'run', "master", '--steps=2',
                            '--bench=time_secondary.track_value',
                            '--quick', '--skip-existing-commits',
                            _machine_file=join(tmpdir, 'asv-machine.json'))
    text, err = capfd.readouterr()
    assert 'Running benchmarks.' not in text

    # Check EXISTING and --environment work
    python = "{0[0]}.{0[1]}".format(sys.version_info)
    env_type = tools.get_default_environment_type(conf, python)
    env_spec = ("-E", env_type + ":" + python)
    tools.run_asv_with_conf(conf, 'run', "EXISTING", '--quick',
                            '--bench=time_secondary.track_value',
                            *env_spec,
                            _machine_file=machine_file)

    # Remove the benchmarks.json file and check publish fails

    os.remove(join(tmpdir, "results_workflow", "benchmarks.json"))

    with pytest.raises(util.UserError):
        tools.run_asv_with_conf(conf, 'publish')

[((18, 13, 18, 35), 'asv.util.long_path', 'util.long_path', ({(18, 28, 18, 34): 'tmpdir'}, {}), '(tmpdir)', False, 'from asv import util\n'), ((41, 18, 41, 52), 'os.path.join', 'join', ({(41, 23, 41, 29): 'tmpdir', (41, 31, 41, 37): '"""html"""', (41, 39, 41, 51): '"""index.html"""'}, {}), "(tmpdir, 'html', 'index.html')", False, 'from os.path import join, isfile\n'), ((42, 18, 42, 52), 'os.path.join', 'join', ({(42, 23, 42, 29): 'tmpdir', (42, 31, 42, 37): '"""html"""', (42, 39, 42, 51): '"""index.json"""'}, {}), "(tmpdir, 'html', 'index.json')", False, 'from os.path import join, isfile\n'), ((43, 18, 43, 48), 'os.path.join', 'join', ({(43, 23, 43, 29): 'tmpdir', (43, 31, 43, 37): '"""html"""', (43, 39, 43, 47): '"""asv.js"""'}, {}), "(tmpdir, 'html', 'asv.js')", False, 'from os.path import join, isfile\n'), ((44, 18, 44, 49), 'os.path.join', 'join', ({(44, 23, 44, 29): 'tmpdir', (44, 31, 44, 37): '"""html"""', (44, 39, 44, 48): '"""asv.css"""'}, {}), "(tmpdir, 'html', 'asv.css')", False, 'from os.path import join, isfile\n'), ((57, 15, 57, 28), 'json.load', 'json.load', ({(57, 25, 57, 27): 'fp'}, {}), '(fp)', False, 'import json\n'), ((91, 14, 91, 65), 'os.path.join', 'join', ({(91, 19, 91, 25): 'tmpdir', (91, 27, 91, 45): '"""results_workflow"""', (91, 47, 91, 64): '"""benchmarks.json"""'}, {}), "(tmpdir, 'results_workflow', 'benchmarks.json')", False, 'from os.path import join, isfile\n'), ((93, 9, 93, 38), 'pytest.raises', 'pytest.raises', ({(93, 23, 93, 37): 'util.UserError'}, {}), '(util.UserError)', False, 'import pytest\n'), ((47, 25, 55, 63), 'os.path.join', 'join', ({(47, 30, 47, 36): 'tmpdir', (47, 38, 47, 44): '"""html"""', (47, 46, 47, 54): '"""graphs"""', (47, 56, 47, 69): '"""arch-x86_64"""', (48, 30, 48, 56): '"""asv_dummy_test_package_1"""', (49, 30, 49, 84): "('asv_dummy_test_package_2-' + tools.DUMMY2_VERSIONS[1])", (50, 30, 50, 45): '"""branch-master"""', (51, 30, 51, 50): '"""cpu-Blazingly fast"""', (52, 30, 52, 51): '"""env-SOME_TEST_VAR-1"""', (53, 30, 53, 49): '"""machine-orangutan"""', (54, 30, 54, 44): '"""os-GNU_Linux"""', (54, 46, 54, 56): '"""python-*"""', (54, 58, 54, 69): '"""ram-128GB"""', (55, 30, 55, 62): '"""params_examples.time_skip.json"""'}, {}), "(tmpdir, 'html', 'graphs', 'arch-x86_64', 'asv_dummy_test_package_1', \n    'asv_dummy_test_package_2-' + tools.DUMMY2_VERSIONS[1], 'branch-master',\n    'cpu-Blazingly fast', 'env-SOME_TEST_VAR-1', 'machine-orangutan',\n    'os-GNU_Linux', 'python-*', 'ram-128GB', 'params_examples.time_skip.json')", False, 'from os.path import join, isfile\n'), ((72, 42, 72, 74), 'os.path.join', 'join', ({(72, 47, 72, 53): 'tmpdir', (72, 55, 72, 73): '"""asv-machine.json"""'}, {}), "(tmpdir, 'asv-machine.json')", False, 'from os.path import join, isfile\n'), ((76, 42, 76, 74), 'os.path.join', 'join', ({(76, 47, 76, 53): 'tmpdir', (76, 55, 76, 73): '"""asv-machine.json"""'}, {}), "(tmpdir, 'asv-machine.json')", False, 'from os.path import join, isfile\n'), ((34, 26, 34, 71), 'os.path.join', 'join', ({(34, 31, 34, 37): 'tmpdir', (34, 39, 34, 57): '"""results_workflow"""', (34, 59, 34, 70): '"""orangutan"""'}, {}), "(tmpdir, 'results_workflow', 'orangutan')", False, 'from os.path import join, isfile\n'), ((35, 26, 35, 58), 'os.path.join', 'join', ({(35, 31, 35, 37): 'tmpdir', (35, 39, 35, 57): '"""results_workflow"""'}, {}), "(tmpdir, 'results_workflow')", False, 'from os.path import join, isfile\n')]