Skip to content

Commit

Permalink
Test/coverage (#17724)
Browse files Browse the repository at this point in the history
* working on test coverage

* more tests

* wip
  • Loading branch information
memsharded authored Feb 7, 2025
1 parent 000fdee commit 9a7f9e2
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 248 deletions.
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
comment: false # Disable codecov PR comments -> leave only the checks
ignore:
- "test/performance"
16 changes: 0 additions & 16 deletions conan/test/assets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
import os
import shutil


def copy_assets(src_folder, dst_folder, assets=None):
assets_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "files")
if src_folder:
src_folder = os.path.join(assets_path, src_folder)
assets = assets or os.listdir(src_folder)
for asset in assets:
s = os.path.join(src_folder, asset)
d = os.path.join(dst_folder, asset)
if os.path.isdir(s):
shutil.copytree(s, d)
else:
shutil.copy2(s, d)
42 changes: 2 additions & 40 deletions conan/test/assets/visual_project_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,47 +245,9 @@
}
'''

main_file_17_2015 = '''
//available in 2015 with c++17
#include <iostream>
namespace test::mytest {
struct mystruct {};
};
int main(){
test::mytest::mystruct t;
}
'''


main_file_17_2017 = '''
//only available in visual 2017 with c++17
#include <iostream>
auto hello(){
return 2;
}
int main()
{
if constexpr(true)
std::cout << "Hello World!" << std::endl;
return 0;
}
'''


def get_vs_project_files(std=False):
if not std:
main = main_file
elif std == "cpp17_2015":
main = main_file_17_2015
elif std == "cpp17_2017":
main = main_file_17_2017
else:
raise Exception("Incorrect std")

def get_vs_project_files():
return {"MyProject.sln": sln_file,
"MyProject/MyProject.vcxproj": vcxproj_file,
"MyProject/MyProject.vcxproj.filters": filters_file,
"MyProject/main.cpp": main}
"MyProject/main.cpp": main_file}
32 changes: 0 additions & 32 deletions conan/test/utils/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@
from conan.internal.model.options import Options


class LocalDBMock(object):

def __init__(self, user=None, access_token=None, refresh_token=None):
self.user = user
self.access_token = access_token
self.refresh_token = refresh_token

def get_login(self, _):
return self.user, self.access_token, self.refresh_token

def get_username(self, _):
return self.user

def store(self, user, access_token, refresh_token, _):
self.user = user
self.access_token = access_token
self.refresh_token = refresh_token


class RedirectedInputStream:
"""
Mock for testing. If get_username or get_password is requested will raise
Expand Down Expand Up @@ -67,19 +48,6 @@ def possible_values(self):
return defaultdict(lambda: [])


class MockCppInfo(object):
def __init__(self):
self.bin_paths = []
self.lib_paths = []
self.include_paths = []
self.libs = []
self.cflags = []
self.cxxflags = []
self.defines = []
self.frameworks = []
self.framework_paths = []


class ConanFileMock(ConanFile):
def __init__(self, settings=None, options=None, runner=None, display_name=""):
self.display_name = display_name
Expand Down
4 changes: 0 additions & 4 deletions conan/test/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from conan.internal.model.profile import Profile
from conan.api.model import RecipeReference
from conan.internal.model.settings import Settings
from conan.test.assets import copy_assets
from conan.test.assets.genconanfile import GenConanfile
from conan.test.utils.artifactory import ArtifactoryServer
from conan.test.utils.mocks import RedirectedInputStream
Expand Down Expand Up @@ -661,9 +660,6 @@ def save(self, files, path=None, clean_first=False):
def save_home(self, files):
self.save(files, path=self.cache_folder)

def copy_assets(self, origin_folder, assets=None):
copy_assets(origin_folder, self.current_folder, assets)

# Higher level operations
def remove_all(self):
self.run("remove '*' -c")
Expand Down
21 changes: 0 additions & 21 deletions conans/client/rest/rest_client_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,27 +277,6 @@ def get_package(self, pref, dest_folder, metadata, only_metadata):
result.update({fn: os.path.join(dest_folder, fn) for fn in files})
return result

@staticmethod
def _is_dir(path, files):
if path == ".":
return True
for the_file in files["files"]:
if path == the_file:
return False
elif the_file.startswith(path):
return True
raise NotFoundException("The specified path doesn't exist")

@staticmethod
def _list_dir_contents(path, files):
ret = []
for the_file in files["files"]:
if path == "." or the_file.startswith(path):
tmp = the_file[len(path) - 1:].split("/", 1)[0]
if tmp not in ret:
ret.append(tmp)
return sorted(ret)

def _upload_files(self, files, urls, ref):
failed = []
uploader = FileUploader(self.requester, self.verify_ssl, self._config)
Expand Down
12 changes: 0 additions & 12 deletions conans/client/rest/rest_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,10 @@ def recipe_revisions(self):
def recipe_revision_file(self):
return '%s/files/{path}' % self.recipe_revision

@property
def packages(self):
return '%s/packages' % self.recipe

@property
def packages_revision(self):
return '%s/packages' % self.recipe_revision

@property
def package(self):
return '%s/{package_id}' % self.packages

@property
def package_files(self):
return '%s/files' % self.package

@property
def package_recipe_revision(self):
"""Route for a package specifying the recipe revision but not the package revision"""
Expand Down
22 changes: 11 additions & 11 deletions test/functional/toolchains/microsoft/test_msbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,12 @@ def check_toolchain_win(self, compiler, version, runtime, cppstd, ide_version):
check_vs_runtime("Release/MyApp.exe", client, ide_version, build_type="Release",
static_runtime=static_runtime)

@pytest.mark.tool("cmake")
@pytest.mark.tool("visual_studio", "16")
@pytest.mark.tool("cmake", "3.23")
@pytest.mark.tool("visual_studio", "17")
def test_toolchain_win_debug(self):
client = TestClient(path_with_spaces=False)
settings = [("compiler", "msvc"),
("compiler.version", "192"),
("compiler.version", "193"),
("compiler.runtime", "dynamic"),
("build_type", "Debug"),
("arch", "x86_64")]
Expand All @@ -514,27 +514,27 @@ def test_toolchain_win_debug(self):
# Run the configure corresponding to this test case
client.run("build . %s" % (settings, ))
assert "conanfile.py: MSBuildToolchain created conantoolchain_debug_x64.props" in client.out
assert f"conanvcvars.bat: Activating environment Visual Studio 16" in client.out
assert f"conanvcvars.bat: Activating environment Visual Studio 17" in client.out
assert "[vcvarsall.bat] Environment initialized for: 'x64'" in client.out
self._run_app(client, "x64", "Debug")
assert "Hello World Debug" in client.out
check_exe_run(client.out, "main", "msvc", "192", "Debug", "x86_64", "14",
check_exe_run(client.out, "main", "msvc", "19", "Debug", "x86_64", "14",
{"DEFINITIONS_BOTH": 'True',
"DEFINITIONS_BOTH2": "True",
"DEFINITIONS_BOTH_INT": "123",
"DEFINITIONS_CONFIG": 'Debug',
"DEFINITIONS_CONFIG2": 'Debug',
"DEFINITIONS_CONFIG_INT": "234"})
check_vs_runtime("x64/Debug/MyApp.exe", client, "16", build_type="Debug")
check_vs_runtime("x64/Debug/MyApp.exe", client, "17", build_type="Debug")

@pytest.mark.tool("cmake")
@pytest.mark.tool("visual_studio", "16")
@pytest.mark.tool("cmake", "3.23")
@pytest.mark.tool("visual_studio", "17")
def test_toolchain_win_multi(self):
ide_version = "16"
ide_version = "17"
client = TestClient(path_with_spaces=False)

settings = [("compiler", "msvc"),
("compiler.version", "192"),
("compiler.version", "193"),
("compiler.cppstd", "17"),
("compiler.runtime", "static")]

Expand Down Expand Up @@ -582,7 +582,7 @@ def test_toolchain_win_multi(self):
assert "[vcvarsall.bat] Environment initialized for: 'x64'" in client.out

self._run_app(client, arch, build_type, shared)
check_exe_run(client.out, "main", "msvc", "192", build_type, arch, "17",
check_exe_run(client.out, "main", "msvc", "19", build_type, arch, "17",
{"DEFINITIONS_BOTH": "True",
"DEFINITIONS_CONFIG": build_type})

Expand Down
21 changes: 5 additions & 16 deletions test/integration/conanfile/runner_test.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import os
import textwrap
import unittest

from conan.test.utils.tools import TestClient


class RunnerTest(unittest.TestCase):

def _install_and_build(self, client, conanfile_text):
files = {"conanfile.py": conanfile_text}
test_folder = os.path.join(client.current_folder, "test_folder")
self.assertFalse(os.path.exists(test_folder))
client.save(files)
client.run("install .")
client.run("build .")
return client
class TestRunner:

def test_ignore_error(self):
conanfile = """from conan import ConanFile
Expand All @@ -26,7 +15,7 @@ def source(self):
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("source .")
self.assertIn("RETCODE True", client.out)
assert "RETCODE True" in client.out

def test_runner_capture_output(self):
conanfile = textwrap.dedent("""
Expand All @@ -38,7 +27,7 @@ def source(self):
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("source .")
self.assertIn("hello Conan!", client.out)
assert "hello Conan!" in client.out

def test_custom_stream_error(self):
# https://github.com/conan-io/conan/issues/7888
Expand All @@ -54,7 +43,7 @@ def source(self):
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("source .")
self.assertIn('conanfile.py: Buffer got msgs Hello', client.out)
assert 'conanfile.py: Buffer got msgs Hello' in client.out

def test_custom_stream_stderr(self):
conanfile = textwrap.dedent("""
Expand All @@ -69,4 +58,4 @@ def source(self):
client = TestClient()
client.save({"conanfile.py": conanfile})
client.run("source .")
self.assertIn('conanfile.py: Buffer got stderr msgs Hello', client.out)
assert 'conanfile.py: Buffer got stderr msgs Hello' in client.out
33 changes: 16 additions & 17 deletions test/integration/environment/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ def build(self):
client.run("install . -s:b os=Windows -s:h os=Linux --build=missing")
# Run the BUILD environment
if platform.system() == "Windows":
cmd = environment_wrap_command(ConanFileMock(),"conanbuildenv", client.current_folder, "mycmake.bat")
cmd = environment_wrap_command(ConanFileMock(), "conanbuildenv", client.current_folder, "mycmake.bat")
client.run_command(cmd)
assert "MYCMAKE=Windows!!" in client.out
assert "MYOPENSSL=Windows!!" in client.out

# Run the RUN environment
if platform.system() != "Windows":
cmd = environment_wrap_command(ConanFileMock(),"conanrunenv", client.current_folder,
cmd = environment_wrap_command(ConanFileMock(), "conanrunenv", client.current_folder,
"mygtest.sh && .{}myrunner.sh".format(os.sep))
client.run_command(cmd, assert_error=not gtest_run_true)
if gtest_run_true:
Expand Down Expand Up @@ -210,7 +210,7 @@ def generate(self):
client.run("install . -pr=myprofile")
# Run the BUILD environment
ext = "bat" if platform.system() == "Windows" else "sh" # TODO: Decide on logic .bat vs .sh
cmd = environment_wrap_command(ConanFileMock(),"conanbuildenv", client.current_folder,
cmd = environment_wrap_command(ConanFileMock(), "conanbuildenv", client.current_folder,
"mycompiler.{}".format(ext))
client.run_command(cmd)
assert "MYCOMPILER!!" in client.out
Expand Down Expand Up @@ -621,29 +621,28 @@ class Pkg(ConanFile):
assert os.path.isfile(os.path.join(client.current_folder, "conanrunenv.ps1"))
assert not os.path.isfile(os.path.join(client.current_folder, "conanrunenv.bat"))
for i in range(num_deps):
cmd = environment_wrap_command(ConanFileMock(),"conanrunenv", client.current_folder,
cmd = environment_wrap_command(ConanFileMock(), "conanrunenv", client.current_folder,
"mycompiler{}.bat".format(i))
if num_deps > 50: # to be safe if we change the "num_deps" number
client.run_command(cmd, assert_error=True)
assert "is not recognized as an internal" in client.out
else:
client.run_command(cmd)
assert "MYTOOL {}!!".format(i) in client.out
# if num_deps > 50: # to be safe if we change the "num_deps" number
# client.run_command(cmd, assert_error=True)
# assert "is not recognized as an internal" in client.out
client.run_command(cmd)
assert "MYTOOL {}!!".format(i) in client.out

# Test .bats now
client.save({"conanfile.py": conanfile}, clean_first=True)
client.run("install .")
assert not os.path.isfile(os.path.join(client.current_folder, "conanrunenv.ps1"))
assert os.path.isfile(os.path.join(client.current_folder, "conanrunenv.bat"))
for i in range(num_deps):
cmd = environment_wrap_command(ConanFileMock(),"conanrunenv", client.current_folder,
cmd = environment_wrap_command(ConanFileMock(), "conanrunenv", client.current_folder,
"mycompiler{}.bat".format(i))
if num_deps > 50: # to be safe if we change the "num_deps" number
client.run_command(cmd, assert_error=True)
# This also fails, but without an error message (in my terminal, it kills the terminal!)
else:
client.run_command(cmd)
assert "MYTOOL {}!!".format(i) in client.out
# if num_deps > 50: # to be safe if we change the "num_deps" number
# client.run_command(cmd, assert_error=True)
# # This also fails, but without an error message (in my terminal, it kills the terminal!)
# else:
client.run_command(cmd)
assert "MYTOOL {}!!".format(i) in client.out


def test_profile_build_env_spaces():
Expand Down
5 changes: 1 addition & 4 deletions test/integration/graph/core/graph_manager_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Alias(ConanFile):
self._cache_recipe(ref, conanfile)

@staticmethod
def recipe_consumer(reference=None, requires=None, build_requires=None, tool_requires=None):
def recipe_consumer(reference=None, requires=None, build_requires=None):
path = temp_folder()
path = os.path.join(path, "conanfile.py")
conanfile = GenConanfile()
Expand All @@ -80,9 +80,6 @@ def recipe_consumer(reference=None, requires=None, build_requires=None, tool_req
if build_requires:
for r in build_requires:
conanfile.with_build_requires(r)
if tool_requires:
for r in tool_requires:
conanfile.with_tool_requires(r)
save(path, str(conanfile))
return path

Expand Down
Loading

0 comments on commit 9a7f9e2

Please sign in to comment.