11"""Tests for packaging.command.install_data."""
22import os
3+ import sys
34import sysconfig
5+ import packaging .database
46from sysconfig import _get_default_scheme
57from packaging .tests import unittest , support
68from packaging .command .install_data import install_data
9+ from packaging .command .install_dist import install_dist
10+ from packaging .command .install_distinfo import install_distinfo
711
812
913class InstallDataTestCase (support .TempdirManager ,
1014 support .LoggingCatcher ,
1115 unittest .TestCase ):
1216
13- def test_simple_run (self ):
17+ def setUp (self ):
18+ super (InstallDataTestCase , self ).setUp ()
1419 scheme = _get_default_scheme ()
1520 old_items = sysconfig ._SCHEMES .items (scheme )
21+
1622 def restore ():
1723 sysconfig ._SCHEMES .remove_section (scheme )
1824 sysconfig ._SCHEMES .add_section (scheme )
1925 for option , value in old_items :
2026 sysconfig ._SCHEMES .set (scheme , option , value )
27+
2128 self .addCleanup (restore )
2229
30+ def test_simple_run (self ):
2331 pkg_dir , dist = self .create_dist ()
2432 cmd = install_data (dist )
2533 cmd .install_dir = inst = os .path .join (pkg_dir , 'inst' )
34+ scheme = _get_default_scheme ()
2635
2736 sysconfig ._SCHEMES .set (scheme , 'inst' ,
2837 os .path .join (pkg_dir , 'inst' ))
@@ -67,8 +76,7 @@ def restore():
6776 three = os .path .join (cmd .install_dir , 'three' )
6877 self .write_file (three , 'xx' )
6978
70- sysconfig ._SCHEMES .set (scheme , 'inst3' ,
71- cmd .install_dir )
79+ sysconfig ._SCHEMES .set (scheme , 'inst3' , cmd .install_dir )
7280
7381 cmd .data_files = {one : '{inst}/one' , two : '{inst2}/two' ,
7482 three : '{inst3}/three' }
@@ -80,6 +88,49 @@ def restore():
8088 self .assertTrue (os .path .exists (os .path .join (inst2 , rtwo )))
8189 self .assertTrue (os .path .exists (os .path .join (inst , rone )))
8290
91+ def test_resources (self ):
92+ install_dir = self .mkdtemp ()
93+ scripts_dir = self .mkdtemp ()
94+ project_dir , dist = self .create_dist (
95+ name = 'Spamlib' , version = '0.1' ,
96+ data_files = {'spamd' : '{scripts}/spamd' })
97+
98+ os .chdir (project_dir )
99+ self .write_file ('spamd' , '# Python script' )
100+ sysconfig ._SCHEMES .set (_get_default_scheme (), 'scripts' , scripts_dir )
101+ sys .path .insert (0 , install_dir )
102+ self .addCleanup (sys .path .remove , install_dir )
103+
104+ cmd = install_dist (dist )
105+ cmd .outputs = ['spamd' ]
106+ cmd .install_lib = install_dir
107+ dist .command_obj ['install_dist' ] = cmd
108+
109+ cmd = install_data (dist )
110+ cmd .install_dir = install_dir
111+ cmd .ensure_finalized ()
112+ dist .command_obj ['install_data' ] = cmd
113+ cmd .run ()
114+
115+ cmd = install_distinfo (dist )
116+ cmd .ensure_finalized ()
117+ dist .command_obj ['install_distinfo' ] = cmd
118+ cmd .run ()
119+
120+ fn = os .path .join (install_dir , 'Spamlib-0.1.dist-info' , 'RESOURCES' )
121+ with open (fn , encoding = 'utf-8' ) as fp :
122+ content = fp .read ().strip ()
123+
124+ expected = 'spamd,%s' % os .path .join (scripts_dir , 'spamd' )
125+ self .assertEqual (content , expected )
126+
127+ # just to be sure, we also test that get_file works here, even though
128+ # packaging.database has its own test file
129+ with packaging .database .get_file ('Spamlib' , 'spamd' ) as fp :
130+ content = fp .read ()
131+
132+ self .assertEqual ('# Python script' , content )
133+
83134
84135def test_suite ():
85136 return unittest .makeSuite (InstallDataTestCase )
0 commit comments