Skip to content

Commit

Permalink
Refactor/tests (#8679)
Browse files Browse the repository at this point in the history
* moving artifactory

* minor refactors

* review, moving tools.vcvars to another folder
  • Loading branch information
memsharded committed Mar 23, 2021
1 parent 8946a07 commit f213fed
Show file tree
Hide file tree
Showing 24 changed files with 194 additions and 226 deletions.
Empty file.
Empty file.
File renamed without changes.
File renamed without changes.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import json
import os
import textwrap
import unittest

import pytest

from conans.client import tools
from conans.client.runner import ConanRunner
from conans.model.ref import ConanFileReference
from conans.paths import RUN_LOG_NAME
from conans.test.assets.cpp_test_files import cpp_hello_conan_files
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.test_files import temp_folder
from conans.test.utils.tools import TestClient, TestServer
from conans.test.utils.mocks import TestBufferConanOutput
Expand All @@ -24,24 +24,24 @@ def setUp(self):
def test_run_log_file_packaged(self):
"""Check if the log file is generated and packaged"""

base = '''
from conans import ConanFile
base = textwrap.dedent("""
from conans import ConanFile
class HelloConan(ConanFile):
name = "Hello0"
version = "0.1"
class HelloConan(ConanFile):
name = "Hello0"
version = "0.1"
def build(self):
self.run('echo "Simulating cmake..."')
def build(self):
self.run('echo "Simulating cmake..."')
def package(self):
self.copy(pattern="%s", dst="", keep_path=False)
''' % RUN_LOG_NAME
def package(self):
self.copy(pattern="%s", dst="", keep_path=False)
""" % RUN_LOG_NAME)

def _install_a_package(print_commands_to_output, generate_run_log_file):
output = TestBufferConanOutput()
out = TestBufferConanOutput()
runner = ConanRunner(print_commands_to_output, generate_run_log_file,
log_run_to_output=True, output=output)
log_run_to_output=True, output=out)

client = TestClient(servers=self.servers,
users={"default": [("lasote", "mypass")]},
Expand All @@ -52,8 +52,8 @@ def _install_a_package(print_commands_to_output, generate_run_log_file):
package_dir = client.cache.package_layout(ref).packages()
package_dir = os.path.join(package_dir, os.listdir(package_dir)[0])
log_file_packaged_ = os.path.join(package_dir, RUN_LOG_NAME)
output = "\n".join([str(output), str(client.out)])
return log_file_packaged_, output
out = "\n".join([str(out), str(client.out)])
return log_file_packaged_, out

log_file_packaged, output = _install_a_package(False, True)
self.assertIn("Packaged 1 '.log' file: conan_run.log", output)
Expand All @@ -73,16 +73,15 @@ def _install_a_package(print_commands_to_output, generate_run_log_file):
self.assertNotIn("Packaged 1 '.log' file: conan_run.log", output)
self.assertFalse(os.path.exists(log_file_packaged))

@pytest.mark.tool_compiler # Needed only because it assume that a settings.compiler is detected
def test_trace_actions(self):
client = TestClient(servers=self.servers,
users={"default": [("lasote", "mypass")]})
trace_file = os.path.join(temp_folder(), "conan_trace.log")
with tools.environment_append({"CONAN_TRACE_FILE": trace_file}):
# UPLOAD A PACKAGE
ref = ConanFileReference.loads("Hello0/0.1@lasote/stable")
files = cpp_hello_conan_files("Hello0", "0.1", need_patch=True, build=False)
client.save(files)
client.save({"conanfile.py": GenConanfile("Hello0", "0.1").with_exports("*"),
"file.txt": "content"})
client.run("user lasote -p mypass -r default")
client.run("export . lasote/stable")
client.run("install %s --build missing" % str(ref))
Expand All @@ -95,11 +94,12 @@ def test_trace_actions(self):
self.assertIn('"X-Client-Anonymous-Id": "**********"', traces)
actions = traces.splitlines()
without_rest_api = [it for it in actions if "REST_API_CALL" not in it]
self.assertTrue(len(without_rest_api) == 11)
assert len(without_rest_api) == 11
for trace in actions:
doc = json.loads(trace)
self.assertIn("_action", doc) # Valid jsons

print(without_rest_api)
self.assertEqual(json.loads(without_rest_api[0])["_action"], "COMMAND")
self.assertEqual(json.loads(without_rest_api[0])["name"], "authenticate")
self.assertEqual(json.loads(without_rest_api[2])["_action"], "COMMAND")
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import textwrap
import time
import unittest
from collections import OrderedDict
Expand All @@ -8,21 +7,16 @@

from conans.model.ref import ConanFileReference
from conans.paths import CONANFILE
from conans.test.assets.cpp_test_files import cpp_hello_conan_files
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient, TestServer
from conans.util.env_reader import get_env


class ExportsSourcesMissingTest(unittest.TestCase):

def test_exports_sources_missing(self):
client = TestClient(default_server_user=True)
conanfile = textwrap.dedent("""
from conans import ConanFile
class Pkg(ConanFile):
exports_sources = "*"
""")
client.save({"conanfile.py": conanfile,
client.save({"conanfile.py": GenConanfile().with_exports_sources("*"),
"source.txt": "somesource"})
client.run("create . pkg/0.1@user/testing")
client.run("upload pkg/0.1@user/testing --all")
Expand Down Expand Up @@ -54,15 +48,12 @@ class Pkg(ConanFile):
client2.out)


@pytest.mark.tool_compiler # Needed only because it assume that a settings.compiler is detected
class MultiRemotesTest(unittest.TestCase):

def setUp(self):
default_server = TestServer()
local_server = TestServer()
self.servers = OrderedDict()
self.servers["default"] = default_server
self.servers["local"] = local_server
self.servers["default"] = TestServer()
self.servers["local"] = TestServer()

def test_list_ref_no_remote(self):
client = TestClient(servers=self.servers)
Expand All @@ -78,13 +69,10 @@ def test_list_ref_no_remote(self):
self.assertIn("pkg/1.0:5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9: None", client.out)

@staticmethod
def _create(client, number, version, deps=None, export=True, modifier=""):
files = cpp_hello_conan_files(number, version, deps, build=False)
# To avoid building
files = {CONANFILE: files[CONANFILE].replace("config(", "config2(") + modifier}
def _create(client, number, version, modifier=""):
files = {CONANFILE: str(GenConanfile(number, version)) + modifier}
client.save(files, clean_first=True)
if export:
client.run("export . lasote/stable")
client.run("export . lasote/stable")

def test_conan_install_build_flag(self):
"""
Expand Down Expand Up @@ -205,8 +193,7 @@ def setUp(self):
self.client = TestClient(servers=self.servers, users=self.users)

def test_predefine_remote(self):
files = cpp_hello_conan_files("Hello0", "0.1", build=False)
self.client.save(files)
self.client.save({"conanfile.py": GenConanfile("Hello0", "0.1")})
self.client.run("export . lasote/stable")
self.client.run("upload Hello0/0.1@lasote/stable -r=remote0")
self.client.run("upload Hello0/0.1@lasote/stable -r=remote1")
Expand All @@ -221,8 +208,7 @@ def test_predefine_remote(self):

def test_upload(self):
ref = ConanFileReference.loads("Hello0/0.1@lasote/stable")
files = cpp_hello_conan_files("Hello0", "0.1", build=False)
self.client.save(files)
self.client.save({"conanfile.py": GenConanfile("Hello0", "0.1")})
self.client.run("export . lasote/stable")
self.client.run("upload %s" % str(ref))

Expand Down Expand Up @@ -259,8 +245,7 @@ def test_fail_when_not_notfound(self):
servers["s2"] = TestServer()

client = TestClient(servers=servers, users=self.users)
files = cpp_hello_conan_files("MyLib", "0.1", build=False)
client.save(files)
client.save({"conanfile.py": GenConanfile("MyLib", "0.1")})
client.run("create . lasote/testing")
client.run("user lasote -p mypass -r s1")
client.run("upload MyLib* -r s1 -c")
Expand All @@ -278,8 +263,7 @@ def test_fail_when_not_notfound(self):
def test_install_from_remotes(self):
for i in range(3):
ref = ConanFileReference.loads("Hello%d/0.1@lasote/stable" % i)
files = cpp_hello_conan_files("Hello%d" % i, "0.1", build=False)
self.client.save(files)
self.client.save({"conanfile.py": GenConanfile("Hello%d" % i, "0.1")})
self.client.run("export . lasote/stable")
self.client.run("upload %s -r=remote%d" % (str(ref), i))

Expand All @@ -288,11 +272,9 @@ def test_install_from_remotes(self):

# Now install it in other machine from remote 0
client2 = TestClient(servers=self.servers, users=self.users)
files = cpp_hello_conan_files("HelloX", "0.1", deps=["Hello0/0.1@lasote/stable",
"Hello1/0.1@lasote/stable",
"Hello2/0.1@lasote/stable"])
files["conanfile.py"] = files["conanfile.py"].replace("def build(", "def build2(")
client2.save(files)

refs = ["Hello0/0.1@lasote/stable", "Hello1/0.1@lasote/stable", "Hello2/0.1@lasote/stable"]
client2.save({"conanfile.py": GenConanfile("HelloX", "0.1").with_requires(*refs)})
client2.run("install . --build=missing")
self.assertIn("Hello0/0.1@lasote/stable from 'remote0'", client2.out)
self.assertIn("Hello1/0.1@lasote/stable from 'remote1'", client2.out)
Expand All @@ -302,18 +284,14 @@ def test_install_from_remotes(self):
self.assertIn("Remote: remote1=http://", client2.out)
self.assertIn("Remote: remote2=http://", client2.out)

@pytest.mark.skipif(TestClient().cache.config.revisions_enabled,
reason="This test is not valid for revisions, where we keep iterating the remotes "
"for searching a package for the same recipe revision")
@pytest.mark.skipif(not get_env("TESTING_REVISIONS_ENABLED", False),
reason="This test is not valid for revisions, where we keep iterating the "
"remotes for searching a package for the same recipe revision")
def test_package_binary_remote(self):
# https://github.com/conan-io/conan/issues/3882
conanfile = """from conans import ConanFile
class ConanFileToolsTest(ConanFile):
pass
"""
# Upload recipe + package to remote1 and remote2
reference = "Hello/0.1@lasote/stable"
self.client.save({"conanfile.py": conanfile})
self.client.save({"conanfile.py": GenConanfile()})
self.client.run("create . %s" % reference)
self.client.run("upload %s -r=remote0 --all" % reference)
self.client.run("upload %s -r=remote2 --all" % reference)
Expand All @@ -340,7 +318,7 @@ class ConanFileToolsTest(ConanFile):

client2 = TestClient(servers=self.servers, users=self.users)
time.sleep(1) # Make sure timestamps increase
client2.save({"conanfile.py": conanfile + " # Comment"})
client2.save({"conanfile.py": str(GenConanfile()) + " # Comment"})
client2.run("create . %s" % reference)
client2.run("upload %s -r=remote2 --all" % reference)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from conans.model.build_info import DEFAULT_LIB
from conans.model.ref import ConanFileReference
from conans.test.assets.cpp_test_files import cpp_hello_conan_files
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient, TestServer
from conans.util.files import save

Expand All @@ -18,11 +18,9 @@ def setUp(self):
self.servers = {"default": TestServer()}
self.client = TestClient(servers=self.servers)

@pytest.mark.tool_compiler
def test_simple_fields(self):
# Result of a create
files = cpp_hello_conan_files("CC", "1.0", build=False)
self.client.save(files, clean_first=True)
self.client.save({"conanfile.py": GenConanfile("CC", "1.0")}, clean_first=True)
self.client.run("create . private_user/channel --json=myfile.json")
my_json = json.loads(self.client.load("myfile.json"))
self.assertFalse(my_json["error"])
Expand Down Expand Up @@ -90,12 +88,11 @@ def test_errors(self):
self.assertEqual(len(my_json["installed"]), 1)
self.assertFalse(my_json["installed"][0]["recipe"]["downloaded"])
self.assertEqual(my_json["installed"][0]["recipe"]["error"],
{'type': 'missing', 'remote': None,
{'type': 'missing', 'remote': None,
'description': "Unable to find 'CC/1.0@private_user/channel' in remotes"})

# Missing binary package
files = cpp_hello_conan_files("CC", "1.0", build=False)
self.client.save(files, clean_first=True)
self.client.save({"conanfile.py": GenConanfile("CC", "1.0")}, clean_first=True)
self.client.run("create . private_user/channel --json=myfile.json ")
self.client.run("upload CC/1.0@private_user/channel -c")
self.client.run("remove '*' -f")
Expand All @@ -114,46 +111,42 @@ def test_errors(self):
my_json["installed"][0]["packages"][0]["error"]["description"])

# Error building
files["conanfile.py"] = files["conanfile.py"].replace("def build2(self):",
"""
conanfile = str(GenConanfile("CC", "1.0")) + """
def build(self):
raise Exception("Build error!")
""")

self.client.save(files, clean_first=True)
"""
self.client.save({"conanfile.py": conanfile}, clean_first=True)
self.client.run("create . private_user/channel --json=myfile.json ", assert_error=True)
my_json = json.loads(self.client.load("myfile.json"))
self.assertTrue(my_json["error"])
self.assertEqual(my_json["installed"][0]["packages"][0]["error"]["type"], "building")
self.assertIsNone(my_json["installed"][0]["packages"][0]["error"]["remote"])
self.assertIn("CC/1.0@private_user/channel: Error in build() method, line 36",
self.assertIn("CC/1.0@private_user/channel: Error in build() method, line 6",
my_json["installed"][0]["packages"][0]["error"]["description"])

@pytest.mark.tool_compiler
def test_json_generation(self):

files = cpp_hello_conan_files("CC", "1.0", build=False)
self.client.save(files, clean_first=True)
self.client.save({"conanfile.py": GenConanfile("CC", "1.0").
with_option("static", [True, False]).with_default_option("static", True)},
clean_first=True)
self.client.run("create . private_user/channel --json=myfile.json ")

self.client.run('upload "*" -c --all')

files = cpp_hello_conan_files("BB", "1.0", build=False)
files["conanfile.py"] += """
conanfile = str(GenConanfile("BB", "1.0")) + """
def configure(self):
self.options["CC"].static = False
def build_requirements(self):
self.build_requires("CC/1.0@private_user/channel")
"""
self.client.save(files, clean_first=True)
self.client.save({"conanfile.py": conanfile}, clean_first=True)
self.client.run("create . private_user/channel --build missing")
self.client.run('upload "*" -c --all')

files = cpp_hello_conan_files("AA", "1.0",
deps=["BB/1.0@private_user/channel"], build=False)
self.client.save(files, clean_first=True)
self.client.save({"conanfile.py": GenConanfile("AA", "1.0").
with_require("BB/1.0@private_user/channel")},
clean_first=True)
self.client.run("create . private_user/channel")
self.client.run('upload "*" -c --all')

Expand All @@ -163,9 +156,8 @@ def build_requirements(self):
[build_requires]
AA*: CC/1.0@private_user/channel
""")
files = cpp_hello_conan_files("PROJECT", "1.0",
deps=["AA/1.0@private_user/channel"], build=False)
self.client.save(files, clean_first=True)
self.client.save({"conanfile.py": GenConanfile("PROJECT", "1.0").
with_require("AA/1.0@private_user/channel")}, clean_first=True)
self.client.run("install . --profile mybr --json=myfile.json --build AA --build BB")
my_json = self.client.load("myfile.json")
my_json = json.loads(my_json)
Expand Down

0 comments on commit f213fed

Please sign in to comment.