11#!/usr/bin/env python3
22
33import os
4+ import platform
45import subprocess
6+ import sys
57import unittest
6- from glob import glob
78from pathlib import Path
89
10+ from rules_python .python .runfiles import runfiles
11+
912
1013class PipRepositoryAnnotationsTest (unittest .TestCase ):
1114 maxDiff = None
@@ -16,43 +19,77 @@ def wheel_pkg_dir(self) -> str:
1619 return env
1720
1821 def test_build_content_and_data (self ):
19- generated_file = (
20- Path .cwd () / "external" / self .wheel_pkg_dir () / "generated_file.txt"
22+ r = runfiles .Create ()
23+ rpath = r .Rlocation (
24+ "pip_repository_annotations_example/external/{}/generated_file.txt" .format (
25+ self .wheel_pkg_dir ()
26+ )
2127 )
28+ generated_file = Path (rpath )
2229 self .assertTrue (generated_file .exists ())
2330
2431 content = generated_file .read_text ().rstrip ()
2532 self .assertEqual (content , "Hello world from build content file" )
2633
2734 def test_copy_files (self ):
28- copied_file = (
29- Path .cwd () / "external" / self .wheel_pkg_dir () / "copied_content/file.txt"
35+ r = runfiles .Create ()
36+ rpath = r .Rlocation (
37+ "pip_repository_annotations_example/external/{}/copied_content/file.txt" .format (
38+ self .wheel_pkg_dir ()
39+ )
3040 )
41+ copied_file = Path (rpath )
3142 self .assertTrue (copied_file .exists ())
3243
3344 content = copied_file .read_text ().rstrip ()
3445 self .assertEqual (content , "Hello world from copied file" )
3546
3647 def test_copy_executables (self ):
37- executable = (
38- Path .cwd ()
39- / "external"
40- / self .wheel_pkg_dir ()
41- / "copied_content/executable.py"
48+ r = runfiles .Create ()
49+ rpath = r .Rlocation (
50+ "pip_repository_annotations_example/external/{}/copied_content/executable{}" .format (
51+ self .wheel_pkg_dir (),
52+ ".exe" if platform .system () == "windows" else ".py" ,
53+ )
4254 )
55+ executable = Path (rpath )
4356 self .assertTrue (executable .exists ())
4457
4558 proc = subprocess .run (
46- [executable ], check = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE
59+ [sys .executable , str (executable )],
60+ check = True ,
61+ stdout = subprocess .PIPE ,
62+ stderr = subprocess .PIPE ,
4763 )
4864 stdout = proc .stdout .decode ("utf-8" ).strip ()
4965 self .assertEqual (stdout , "Hello world from copied executable" )
5066
5167 def test_data_exclude_glob (self ):
52- files = glob ("external/" + self .wheel_pkg_dir () + "/wheel-*.dist-info/*" )
53- basenames = [Path (path ).name for path in files ]
54- self .assertIn ("WHEEL" , basenames )
55- self .assertNotIn ("RECORD" , basenames )
68+ current_wheel_version = "0.37.1"
69+
70+ r = runfiles .Create ()
71+ dist_info_dir = (
72+ "pip_repository_annotations_example/external/{}/wheel-{}.dist-info" .format (
73+ self .wheel_pkg_dir (),
74+ current_wheel_version ,
75+ )
76+ )
77+
78+ # `WHEEL` is expected to be there to show dist-info files are included in the runfiles
79+ wheel_path = r .Rlocation ("{}/WHEEL" .format (dist_info_dir ))
80+
81+ # However, `RECORD` was explicitly excluded, so it should be missing
82+ record_path = r .Rlocation ("{}/RECORD" .format (dist_info_dir ))
83+
84+ # Because windows does not have `--enable_runfiles` on by default, the
85+ # `runfiles.Rlocation` results will be different on this platform vs
86+ # unix platforms. See `@rules_python//python/runfiles` for more details.
87+ if platform .system () == "Windows" :
88+ self .assertIsNotNone (wheel_path )
89+ self .assertIsNone (record_path )
90+ else :
91+ self .assertTrue (Path (wheel_path ).exists ())
92+ self .assertFalse (Path (record_path ).exists ())
5693
5794
5895if __name__ == "__main__" :
0 commit comments