Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. diff --git a/load.c b/load.c
  2. index 00ccb6c..580b9f1 100644
  3. --- a/load.c
  4. +++ b/load.c
  5. @@ -380,9 +380,9 @@ search_required(VALUE fname, volatile VALUE *path)
  6. if ((tmp = rb_find_file(fname)) != 0) {
  7. tmp = rb_file_expand_path(tmp, Qnil);
  8. ext = strrchr(ftptr = RSTRING_PTR(tmp), '.');
  9. -// if (!rb_feature_p(ftptr, ext, Qtrue, Qtrue, 0)) {
  10. + // if (!rb_feature_p(ftptr, ext, Qtrue, Qtrue, 0)) {
  11. *path = tmp;
  12. -// }
  13. + // }
  14. return 'r';
  15. }
  16. return 0;
  17. diff --git a/objc.m b/objc.m
  18. index e098a86..a0bccc8 100644
  19. --- a/objc.m
  20. +++ b/objc.m
  21. @@ -192,13 +192,42 @@ rb_objc_symbolize_address(void *addr, void **start, char *name,
  22. VALUE
  23. rb_file_expand_path(VALUE fname, VALUE dname)
  24. {
  25. - NSString *res = [(NSString *)fname stringByExpandingTildeInPath];
  26. - if (![res isAbsolutePath]) {
  27. - NSString *dir = dname != Qnil
  28. - ? (NSString *)dname
  29. - : [[NSFileManager defaultManager] currentDirectoryPath];
  30. - res = [dir stringByAppendingPathComponent:res];
  31. + NSString *res = (NSString *)StringValue(fname);
  32. +
  33. + // If the original path starts with "/private", stringByStandardizingPath:
  34. + // and stringByResolvingSymlinksInPath: remove the "/private" prefix if that path resolves
  35. + // to a directory in "/". MRI, however, doesn't do that. So we replace the prefix with our own
  36. + // so the Cocoa methods won't treat it as "/private" and replace it again in the end _if_
  37. + // the expanded path still has our prefix.
  38. + BOOL private = [res hasPrefix:@"/private"];
  39. + if (private) {
  40. + res = [@"/__mr_private__" stringByAppendingPathComponent:[res substringFromIndex:8]];
  41. }
  42. +
  43. + if ([res isAbsolutePath]) {
  44. + res = [res stringByResolvingSymlinksInPath];
  45. + } else {
  46. + NSString *dir = dname != Qnil ?
  47. + (NSString *)dname : [[NSFileManager defaultManager] currentDirectoryPath];
  48. +
  49. + if (![dir isAbsolutePath]) {
  50. + dir = (NSString *)rb_file_expand_path((VALUE)dir, Qnil);
  51. + }
  52. +
  53. + // stringByStandardizingPath does not expand "/." to "/"
  54. + if ([res isEqualTo:@"."] && [dir isEqualTo:@"/"]) {
  55. + return (VALUE)[@"/" mutableCopy];
  56. + } else {
  57. + res = [dir stringByAppendingPathComponent:res];
  58. + }
  59. +
  60. + res = [res stringByStandardizingPath];
  61. + }
  62. +
  63. + if (private && [res hasPrefix:@"/__mr_private__"]) {
  64. + res = [@"/private" stringByAppendingPathComponent:[res substringFromIndex:15]];
  65. + }
  66. +
  67. return (VALUE)[res mutableCopy];
  68. }
  69.  
  70. diff --git a/spec/frozen/core/file/expand_path_spec.rb b/spec/frozen/core/file/expand_path_spec.rb
  71. index e896ac4..f07868e 100644
  72. --- a/spec/frozen/core/file/expand_path_spec.rb
  73. +++ b/spec/frozen/core/file/expand_path_spec.rb
  74. @@ -67,10 +67,16 @@ describe "File.expand_path" do
  75. File.expand_path('~/').should == ENV['HOME']
  76. File.expand_path('~/..badfilename').should == "#{ENV['HOME']}/..badfilename"
  77. File.expand_path('..').should == Dir.pwd.split('/')[0...-1].join("/")
  78. - File.expand_path('//').should == '//'
  79. File.expand_path('~/a','~/b').should == "#{ENV['HOME']}/a"
  80. end
  81.  
  82. + not_compliant_on :macruby do
  83. + it "leaves multiple prefixed slashes untouched" do
  84. + File.expand_path('//').should == '//'
  85. + File.expand_path('////').should == '////'
  86. + end
  87. + end
  88. +
  89. it "raises an ArgumentError if the path is not valid" do
  90. lambda { File.expand_path("~a_fake_file") }.should raise_error(ArgumentError)
  91. end
  92. @@ -85,7 +91,7 @@ describe "File.expand_path" do
  93. end
  94. end
  95.  
  96. - it "raises an ArgumentError is not passed one or two arguments" do
  97. + it "raises an ArgumentError if not passed one or two arguments" do
  98. lambda { File.expand_path }.should raise_error(ArgumentError)
  99. lambda { File.expand_path '../', 'tmp', 'foo' }.should raise_error(ArgumentError)
  100. end
  101. diff --git a/spec/frozen/tags/macruby/core/file/expand_path_tags.txt b/spec/frozen/tags/macruby/core/file/expand_path_tags.txt
  102. index 26a751e..b33331c 100644
  103. --- a/spec/frozen/tags/macruby/core/file/expand_path_tags.txt
  104. +++ b/spec/frozen/tags/macruby/core/file/expand_path_tags.txt
  105. @@ -1,7 +1,2 @@
  106. -critical:File.expand_path raises an ArgumentError is not passed one or two arguments
  107. critical:File.expand_path raises a TypeError if not passed a String type
  108. -fails:File.expand_path converts a pathname to an absolute pathname, using a complete path
  109. -fails:File.expand_path expand path with
  110. -fails:File.expand_path expand_path for commoms unix path give a full path
  111. -fails:File.expand_path raises an ArgumentError if the path is not valid
  112. -fails:File.expand_path expands /./dir to /dir
  113. +fails:File.expand_path raises an ArgumentError if the path is not valid
  114. \ No newline at end of file
  115. diff --git a/spec/macruby/core/file_spec.rb b/spec/macruby/core/file_spec.rb
  116. new file mode 100644
  117. index 0000000..47c1696
  118. --- /dev/null
  119. +++ b/spec/macruby/core/file_spec.rb
  120. @@ -0,0 +1,26 @@
  121. +# TODO
  122. +# require File.expand_path('../spec_helper', __FILE__)
  123. +# require File.expand_path('../spec_helper.rb', __FILE__)
  124. +
  125. +require File.dirname(__FILE__) + "/../spec_helper"
  126. +
  127. +describe "File#expand_path" do
  128. + it "returns path prefixed with '/private/tmp' if the original path has the '/private' prefix and expands to '/tmp'" do
  129. + File.expand_path('/private/tmp').should == '/private/tmp'
  130. + File.expand_path('/private/bin').should == '/private/bin'
  131. + File.expand_path('/private/foo').should == '/private/foo'
  132. + File.expand_path('/private/../foo').should == '/foo'
  133. + File.expand_path('/private/tmp/foo').should == '/private/tmp/foo'
  134. + File.expand_path('/private/bin/foo').should == '/private/bin/foo'
  135. + File.expand_path('/private/foo/../tmp').should == '/private/tmp'
  136. + File.expand_path('/private/private').should == '/private/private'
  137. + File.expand_path('/private/private/private').should == '/private/private/private'
  138. + end
  139. +
  140. + it "removes prefixed slashes, which isn't done by MRI" do
  141. + File.expand_path('//').should == '/'
  142. + File.expand_path('////').should == '/'
  143. + File.expand_path('////foo').should == '/foo'
  144. + File.expand_path('////foo//bar').should == '/foo/bar'
  145. + end
  146. +end
  147. \ No newline at end of file
Add Comment
Please, Sign In to add comment