Repository: conan-io/conan

Issue:
Make self.user and self.channel available in test_package
Right now, they are not defined while test_package/conanfile.py is being run. It errors with:
```
ConanException: CONAN_USERNAME environment variable not defined, but self.user is used in conanfile
```

As user/channel are already available, it could be assigned.




Discussion:

Resolving patch:
diff --git a/conans/__init__.py b/conans/__init__.py
--- a/conans/__init__.py
+++ b/conans/__init__.py
@@ -15,4 +15,4 @@
 COMPLEX_SEARCH_CAPABILITY = "complex_search"
 SERVER_CAPABILITIES = [COMPLEX_SEARCH_CAPABILITY, ]
 
-__version__ = '1.1.0'
+__version__ = '1.2.0-dev'
diff --git a/conans/client/manager.py b/conans/client/manager.py
--- a/conans/client/manager.py
+++ b/conans/client/manager.py
@@ -236,6 +236,8 @@ def _inject_require(conanfile, inject_require):
             require.conan_reference = require.range_reference = inject_require
         else:
             conanfile.requires(str(inject_require))
+        conanfile._user = inject_require.user
+        conanfile._channel = inject_require.channel
 
     def _get_graph_builder(self, loader, update, remote_proxy):
         local_search = self._search_manager


Test patch:
diff --git a/conans/client/cmd/test.py b/conans/client/cmd/test.py
--- a/conans/client/cmd/test.py
+++ b/conans/client/cmd/test.py
@@ -5,6 +5,7 @@
 from conans.util.files import rmdir
 from conans.util.env_reader import get_env
 
+
 class PackageTester(object):
 
     def __init__(self, manager, user_io):
@@ -26,16 +27,16 @@ def install_build_and_test(self, conanfile_abs_path, reference, profile,
             build_modes = ["never"]
         try:
             self._manager.install(inject_require=reference,
-                                reference=conanfile_abs_path,
-                                install_folder=test_build_folder,
-                                remote=remote,
-                                profile=profile,
-                                update=update,
-                                build_modes=build_modes,
-                                manifest_folder=manifest_folder,
-                                manifest_verify=manifest_verify,
-                                manifest_interactive=manifest_interactive,
-                                keep_build=keep_build)
+                                  reference=conanfile_abs_path,
+                                  install_folder=test_build_folder,
+                                  remote=remote,
+                                  profile=profile,
+                                  update=update,
+                                  build_modes=build_modes,
+                                  manifest_folder=manifest_folder,
+                                  manifest_verify=manifest_verify,
+                                  manifest_interactive=manifest_interactive,
+                                  keep_build=keep_build)
             self._manager.build(conanfile_abs_path, base_folder, test_build_folder, package_folder=None,
                                 install_folder=test_build_folder, test=str(reference))
         finally:
diff --git a/conans/test/integration/same_userchannel_test.py b/conans/test/integration/same_userchannel_test.py
--- a/conans/test/integration/same_userchannel_test.py
+++ b/conans/test/integration/same_userchannel_test.py
@@ -3,6 +3,33 @@
 import os
 
 
+class UserChannelTestPackage(unittest.TestCase):
+
+    def test(self):
+        # https://github.com/conan-io/conan/issues/2501
+        client = TestClient()
+        conanfile = """from conans import ConanFile
+class SayConan(ConanFile):
+    pass
+"""
+        test = """from conans import ConanFile
+class SayConan(ConanFile):
+    def requirements(self):
+        self.output.info("USER: %s!!" % self.user)
+        self.output.info("CHANNEL: %s!!" % self.channel)
+
+    def test(self):
+        pass
+"""
+
+        client.save({"conanfile.py": conanfile,
+                     "test_package/conanfile.py": test})
+        client.run("create . Pkg/0.1@conan/testing")
+        self.assertIn("PROJECT: USER: conan!!", client.out)
+        self.assertIn("PROJECT: CHANNEL: testing!!", client.out)
+
+
+
 class SameUserChannelTest(unittest.TestCase):
 
     def setUp(self):