Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Add test with absolute paths to 'txt' generator #7797

Merged
merged 3 commits into from Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion conans/client/generators/text.py
Expand Up @@ -141,10 +141,16 @@ def _loads_cpp_info(text, filter_empty):
data[dep][config][field] = lines

# Build the data structures
def _relativize_path(p, _rootpath):
try:
return os.path.relpath(p, _rootpath)
except ValueError:
return p

def _populate_cpp_info(_cpp_info, _data, _rootpath):
for key, value in _data.items():
if key.endswith('dirs'):
value = [os.path.relpath(it, _rootpath) for it in value]
value = [_relativize_path(it, _rootpath) for it in value]
value = ['' if it == '.' else it for it in value]
setattr(_cpp_info, key, value)

Expand Down
43 changes: 43 additions & 0 deletions conans/test/unittests/client/generators/txt/test_abs_paths.py
@@ -0,0 +1,43 @@
import platform
import unittest

from conans.client.generators.text import TXTGenerator
from conans.model.build_info import CppInfo
from conans.model.conan_file import ConanFile
from conans.model.env_info import EnvValues
from conans.model.ref import ConanFileReference
from conans.model.settings import Settings
from conans.test.utils.tools import TestBufferConanOutput


class AbsPathsTestCase(unittest.TestCase):

@unittest.skipIf(platform.system() == "Windows", "Uses unix-like paths")
def test_abs_path_unix(self):
conanfile = ConanFile(TestBufferConanOutput(), None)
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("pkg/0.1")
cpp_info = CppInfo(ref.name, "/rootdir")
cpp_info.includedirs = ["/an/absolute/dir"]
cpp_info.filter_empty = False
conanfile.deps_cpp_info.add(ref.name, cpp_info)

master_content = TXTGenerator(conanfile).content
after_cpp_info, _, _, _ = TXTGenerator.loads(master_content, filter_empty=False)
self.assertListEqual(after_cpp_info[ref.name].includedirs, ["../an/absolute/dir"])
self.assertListEqual(after_cpp_info[ref.name].include_paths, ["/rootdir/../an/absolute/dir"])

@unittest.skipUnless(platform.system() == "Windows", "Uses windows-like paths")
def test_absolute_directory(self):
conanfile = ConanFile(TestBufferConanOutput(), None)
conanfile.initialize(Settings({}), EnvValues())
ref = ConanFileReference.loads("pkg/0.1")
cpp_info = CppInfo(ref.name, "C:/my/root/path")
cpp_info.includedirs = ["D:/my/path/to/something"]
cpp_info.filter_empty = False
conanfile.deps_cpp_info.add(ref.name, cpp_info)

master_content = TXTGenerator(conanfile).content
after_cpp_info, _, _, _ = TXTGenerator.loads(master_content, filter_empty=False)
self.assertListEqual(after_cpp_info[ref.name].includedirs, ["D:/my/path/to/something"])
self.assertListEqual(after_cpp_info[ref.name].include_paths, ["D:/my/path/to/something"])