Repository: conda/conda

Issue:
Add test for conda/install.py imports
https://github.com/conda/conda/pull/2526#issuecomment-220751869

Since this type of bug was introduced by at least three people independently, I think it would be good to add a test for this. The test could be something along the lines of:

```
tmp_dir = tempfile.mkdtemp()
shutil.copyfile(conda.install.__file__, tmp_dir)
sys.path = [tmp_dir] # we also need the standard library here
import install
```

Basically, put the `install` module into an empty directory, and test if it can be imported by itself.



Discussion:
Perhaps it would be sufficient to scan for `(from|import)\s+(conda|[.])`?


Resolving patch:
diff --git a/conda/install.py b/conda/install.py
--- a/conda/install.py
+++ b/conda/install.py
@@ -85,10 +85,9 @@ def remove_binstar_tokens(url):
 
     # A simpler version of url_channel will do
     def url_channel(url):
-        return None, 'defaults'
+        return url.rsplit('/', 2)[0] + '/' if url and '/' in url else None, 'defaults'
 
-    # We don't use the package cache or trash logic in the installer
-    pkgs_dirs = []
+    pkgs_dirs = [join(sys.prefix, 'pkgs')]
 
 on_win = bool(sys.platform == "win32")
 
@@ -423,13 +422,14 @@ def create_meta(prefix, dist, info_dir, extra_info):
         meta = json.load(fi)
     # add extra info, add to our intenral cache
     meta.update(extra_info)
+    if 'url' not in meta:
+        meta['url'] = read_url(dist)
     # write into <env>/conda-meta/<dist>.json
     meta_dir = join(prefix, 'conda-meta')
     if not isdir(meta_dir):
         os.makedirs(meta_dir)
     with open(join(meta_dir, _dist2filename(dist, '.json')), 'w') as fo:
         json.dump(meta, fo, indent=2, sort_keys=True)
-    # only update the package cache if it is loaded for this prefix.
     if prefix in linked_data_:
         load_linked_data(prefix, dist, meta)
 
@@ -821,12 +821,15 @@ def load_linked_data(prefix, dist, rec=None):
                 rec = json.load(fi)
         except IOError:
             return None
-        _, schannel = url_channel(rec.get('url'))
     else:
         linked_data(prefix)
+    url = rec.get('url')
+    channel, schannel = url_channel(url)
+    if 'fn' not in rec:
+        rec['fn'] = url.rsplit('/', 1)[-1] if url else dname + '.tar.bz2'
+    rec['channel'] = channel
     rec['schannel'] = schannel
     cprefix = '' if schannel == 'defaults' else schannel + '::'
-    rec['fn'] = dname + '.tar.bz2'
     linked_data_[prefix][str(cprefix + dname)] = rec
     return rec
 
@@ -1159,9 +1162,9 @@ def main():
 
     prefix = opts.prefix
     pkgs_dir = join(prefix, 'pkgs')
+    pkgs_dirs[0] = [pkgs_dir]
     if opts.verbose:
         print("prefix: %r" % prefix)
-    pkgs_dirs.append(pkgs_dir)
 
     if opts.file:
         idists = list(yield_lines(join(prefix, opts.file)))


Test patch:
diff --git a/tests/test_install.py b/tests/test_install.py
--- a/tests/test_install.py
+++ b/tests/test_install.py
@@ -448,5 +448,20 @@ def test_misc(self):
         self.assertEqual(duplicates_to_remove(li, [d1, d2]), [])
 
 
+def test_standalone_import():
+    import sys
+    import conda.install
+    tmp_dir = tempfile.mkdtemp()
+    fname = conda.install.__file__.rstrip('co')
+    shutil.copyfile(fname, join(tmp_dir, basename(fname)))
+    opath = [tmp_dir]
+    opath.extend(s for s in sys.path if basename(s) not in ('conda', 'site-packages'))
+    opath, sys.path = sys.path, opath
+    try:
+        import install
+    finally:
+        sys.path = opath
+
+
 if __name__ == '__main__':
     unittest.main()