Forum: Ruby-core Question about r39944

38b74ccb8896076ccd0adbab2eba0809?d=identicon&s=25 Aaron Patterson (tenderlove)
on 2013-04-05 18:25
(Received via mailing list)
Hi,

@xibbar changed test/psych/** to do "require_relative 'helper'".  Why?
The "test" directory should be on the load path, so `require_relative`
does not seem necessary.

Thanks!
9361878d459f1709feec780518946ee5?d=identicon&s=25 NARUSE, Yui (Guest)
on 2013-04-05 20:29
(Received via mailing list)
Even if test directory should be on the load path on test-all, you
should
make tests standalone
because we sometimes run tests without test-all to debug it as small set
as
it can be.



2013/4/6 Aaron Patterson <tenderlove@ruby-lang.org>
87e61e351b823e19b4a62a896d2af0d4?d=identicon&s=25 Henry Maddocks (Guest)
on 2013-04-05 23:39
(Received via mailing list)
On 6/04/2013, at 7:28 AM, "NARUSE, Yui" <naruse@airemix.jp> wrote:

> Even if test directory should be on the load path on test-all, you should make
tests standalone
> because we sometimes run tests without test-all to debug it as small set as it
can be.

Isn't that what the -I command line argument is for?

ruby -Itest test/path/to/test.rb

Henry
38b74ccb8896076ccd0adbab2eba0809?d=identicon&s=25 Aaron Patterson (tenderlove)
on 2013-04-05 23:51
(Received via mailing list)
Can you give me a specific example of how you're running the tests?  It
seems we should make sure the load path is always correct rather than
couple the source file to it's location in the file system.
B11f10c4cd9d53970e7be20caa43f940?d=identicon&s=25 Tanaka Akira (Guest)
on 2013-04-06 01:45
(Received via mailing list)
2013/4/6 Aaron Patterson <tenderlove@ruby-lang.org>:
> Can you give me a specific example of how you're running the tests?  It
> seems we should make sure the load path is always correct rather than
> couple the source file to it's location in the file system.

In general, require_relative is a good tool to require a utility
library for a test.

For example, test/objspace/test_objspace.rb uses test/ruby/envutil.rb.
But I want to invoke the test as follows.

% ./ruby test/objspace/test_objspace.rb

Since test/ruby/ is not a standard library location,
the load path doesn't contain it.

So, if "require" is used instead of "require_relative",
I need to invoke ruby with -Itest/ruby or test_objspace.rb needs to
tweak the load path which is irritative.
2abdb50caf0dc5b510330f68b02db8e4?d=identicon&s=25 Rodrigo Rosenfeld Rosas (Guest)
on 2013-04-06 02:24
(Received via mailing list)
Em 05-04-2013 20:41, Tanaka Akira escreveu:
> % ./ruby test/objspace/test_objspace.rb
>
> Since test/ruby/ is not a standard library location,
> the load path doesn't contain it.
>
> So, if "require" is used instead of "require_relative",
> I need to invoke ruby with -Itest/ruby or test_objspace.rb needs to
> tweak the load path which is irritative.

I agree. I tend to often use require_relative in my specs very often and
I do believe it is a good pattern. Actually I also use require_relative
in some libraries as well. I find it easier in lots of cases to be
sure/explicit about what file I'm loading and avoid any search-path
related confusion.

The reason most tests use require is probably because require_relative
didn't exist until recently.
38b74ccb8896076ccd0adbab2eba0809?d=identicon&s=25 Aaron Patterson (tenderlove)
on 2013-04-06 02:38
(Received via mailing list)
On Sat, Apr 06, 2013 at 08:41:46AM +0900, Tanaka Akira wrote:
> 2013/4/6 Aaron Patterson <tenderlove@ruby-lang.org>:
> > Can you give me a specific example of how you're running the tests?  It
> > seems we should make sure the load path is always correct rather than
> > couple the source file to it's location in the file system.
>
> In general, require_relative is a good tool to require a utility
> library for a test.
>
> For example, test/objspace/test_objspace.rb uses test/ruby/envutil.rb.

Ok.  So if you move `test_objspace.rb` to 'test/foo/bar', now you have
to modify the source of `test_objspace.rb`.  Why does the source file
depend on the file system location?

> But I want to invoke the test as follows.
>
> % ./ruby test/objspace/test_objspace.rb
>
> Since test/ruby/ is not a standard library location,
> the load path doesn't contain it.
>
> So, if "require" is used instead of "require_relative",
> I need to invoke ruby with -Itest/ruby or test_objspace.rb needs to
> tweak the load path which is irritative.

export RUBYOPT='-Itest/ruby'

;-)
B11f10c4cd9d53970e7be20caa43f940?d=identicon&s=25 Tanaka Akira (Guest)
on 2013-04-06 03:22
(Received via mailing list)
2013/4/6 Aaron Patterson <tenderlove@ruby-lang.org>:

> Ok.  So if you move `test_objspace.rb` to 'test/foo/bar', now you have
> to modify the source of `test_objspace.rb`.  Why does the source file
> depend on the file system location?

I think it is better than other ways.

> export RUBYOPT='-Itest/ruby'

It is problematic on multi user system.
Consider someone creates /tmp/test/ruby/rubygems.rb as "puts 'hahaha'"
or
another dangerous program and you run ruby in /tmp.
7d3dfc3ac70b56bd407852c5c69fd0d0?d=identicon&s=25 fujioka (Guest)
on 2013-04-08 04:17
(Received via mailing list)
Hi aaron,

When I usually run the standalone test:

% ruby -e 'require "./test/psych/test_array.rb"'

but before my patch:
% ruby -e 'require "./test/psych/test_array.rb"'
/Users/fujioka/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require': cannot load such file -- psych/helper (LoadError)
  from
/Users/fujioka/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require'
  from /Users/fujioka/build/ruby/test/psych/test_array.rb:1:in `<top
(required)>'
  from
/Users/fujioka/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require'
  from
/Users/fujioka/local/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require'
  from -e:1:in `<main>'

I think the test should pass without special option.
This topic is locked and can not be replied to.