How to best require files in support/?

128 views
Skip to first unread message

Arve Knudsen

unread,
Sep 3, 2010, 8:10:35 AM9/3/10
to cu...@googlegroups.com
Hi

I am trying to split up my code in support/ into several files, and have support/env.rb require the other files. However, when I require another support/*.rb from support/env.rb I see that it gets loaded twice. I take it that Cucumber loads all support/*.rb files automatically? How am I supposed to require other files from support/env.rb (so they are not loaded twice)?

Thanks,
Arve

Matt Wynne

unread,
Sep 5, 2010, 5:15:34 PM9/5/10
to cu...@googlegroups.com
Why do you need to explicity require them? Do they have to be executed in a particular order?

> --
> You received this message because you are subscribed to the Google Groups "Cukes" group.
> To post to this group, send email to cu...@googlegroups.com.
> To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cukes?hl=en.

cheers,
Matt

http://blog.mattwynne.net
+44(0)7974 430184

andywatts

unread,
Sep 7, 2010, 10:44:07 AM9/7/10
to Cukes
I think this page has the answer.
http://wiki.github.com/aslakhellesoy/cucumber/setting-up-selenium

Basically setup profiles in config/cucumber.yml with something like
the following

default: -r features/support/env.rb -r features/support/plain.rb
enhanced: -r features/support/env.rb -r features/support/enhanced.rb

Andy

On Sep 6, 5:15 am, Matt Wynne <m...@mattwynne.net> wrote:
> Why do you need to explicityrequirethem? Do they have to be executed in a particular order?
>
> On 3 Sep 2010, at 13:10, Arve Knudsen wrote:
>
> > Hi
>
> > I am trying to split up my code insupport/ into several files, and havesupport/env.rbrequirethe other files. However, when Irequireanothersupport/*.rb fromsupport/env.rb I see that it gets loaded twice. I take it that Cucumber loads allsupport/*.rb files automatically? How am I supposed torequireother files fromsupport/env.rb (so they are not loaded twice)?
>
> > Thanks,
> > Arve
>
> > --
> > You received this message because you are subscribed to the Google Groups "Cukes" group.
> > To post to this group, send email to cu...@googlegroups.com.
> > To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/cukes?hl=en.

Arve Knudsen

unread,
Sep 7, 2010, 11:23:16 AM9/7/10
to cu...@googlegroups.com
Thanks, I will look into this. I've just had my hands full with work the last few days.

Arve

andywatts

unread,
Sep 8, 2010, 2:11:29 AM9/8/10
to Cukes
AFAIK, not all files in support are loaded like you stated.

Also, when requiring a support file from another file in support, you
may need the following..

require File.expand_path(File.dirname(__FILE__) + '/xyz')

- Andy
> > cukes+un...@googlegroups.com <cukes%2Bunsu...@googlegroups.com>.
> > > > For more options, visit this group athttp://
> > groups.google.com/group/cukes?hl=en.
>
> > > cheers,
> > > Matt
>
> > >http://blog.mattwynne.net
> > > +44(0)7974 430184
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Cukes" group.
> > To post to this group, send email to cu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > cukes+un...@googlegroups.com <cukes%2Bunsu...@googlegroups.com>.

Arve Knudsen

unread,
Sep 8, 2010, 7:42:33 AM9/8/10
to cu...@googlegroups.com
I have attached a case which demonstrates my problem. Basically, I define one World mixin in mixin1.rb and another in mixin2.rb. In order to require them from env.rb I add the support directory to $LOAD_PATH. Basically, requiring them works, but they are loaded for the second time later by Cucumber.

I'm guessing that Ruby/Cucumber don't understand that the files have already been loaded due to their being referred to by different pathnames (by env.rb and Cucumber). Maybe Cucumber needs to be more intelligent and avoid reloading files?

Thanks
Arve

To unsubscribe from this group, send email to cukes+un...@googlegroups.com.
features.zip

aslak hellesoy

unread,
Sep 8, 2010, 9:28:46 AM9/8/10
to cu...@googlegroups.com
On Wed, Sep 8, 2010 at 1:42 PM, Arve Knudsen <arve.k...@gmail.com> wrote:
> I have attached a case which demonstrates my problem. Basically, I define
> one World mixin in mixin1.rb and another in mixin2.rb. In order to require
> them from env.rb I add the support directory to $LOAD_PATH. Basically,
> requiring them works, but they are loaded for the second time later by
> Cucumber.
> I'm guessing that Ruby/Cucumber don't understand that the files have already
> been loaded due to their being referred to by different pathnames (by env.rb
> and Cucumber). Maybe Cucumber needs to be more intelligent and avoid
> reloading files?
>

Ruby doesn't provide a way to ask the runtime: "has this file been
required before?", so Cucumber can't be intelligent about this.
If you want explicit loading and not automatic loading, just put your
files outside the features folder.

Aslak

Matt Wynne

unread,
Sep 8, 2010, 9:35:03 AM9/8/10
to cu...@googlegroups.com
On 8 Sep 2010, at 12:42, Arve Knudsen wrote:

I have attached a case which demonstrates my problem. Basically, I define one World mixin in mixin1.rb and another in mixin2.rb. In order to require them from env.rb I add the support directory to $LOAD_PATH. Basically, requiring them works, but they are loaded for the second time later by Cucumber.

I'm guessing that Ruby/Cucumber don't understand that the files have already been loaded due to their being referred to by different pathnames (by env.rb and Cucumber). Maybe Cucumber needs to be more intelligent and avoid reloading files?

Thanks
Arve

Are you using File.expand_path? If so, doesn't ruby know to not include them a second time when Cucumber calls Kernel#require ?

<features.zip>

Arve Knudsen

unread,
Sep 8, 2010, 9:43:28 AM9/8/10
to cu...@googlegroups.com
On Wed, Sep 8, 2010 at 3:35 PM, Matt Wynne <ma...@mattwynne.net> wrote:

On 8 Sep 2010, at 12:42, Arve Knudsen wrote:

I have attached a case which demonstrates my problem. Basically, I define one World mixin in mixin1.rb and another in mixin2.rb. In order to require them from env.rb I add the support directory to $LOAD_PATH. Basically, requiring them works, but they are loaded for the second time later by Cucumber.

I'm guessing that Ruby/Cucumber don't understand that the files have already been loaded due to their being referred to by different pathnames (by env.rb and Cucumber). Maybe Cucumber needs to be more intelligent and avoid reloading files?

Thanks
Arve

Are you using File.expand_path? If so, doesn't ruby know to not include them a second time when Cucumber calls Kernel#require ?

Instead of requiring absolute paths, which I found actually works (probably due to an implementation detail), I add the support directory to $LOAD_PATH. Using File.expand_path on this directory path doesn't help.

Arve

aslak hellesoy

unread,
Sep 8, 2010, 9:43:55 AM9/8/10
to cu...@googlegroups.com
On Wed, Sep 8, 2010 at 3:35 PM, Matt Wynne <ma...@mattwynne.net> wrote:
>
> On 8 Sep 2010, at 12:42, Arve Knudsen wrote:
>
> I have attached a case which demonstrates my problem. Basically, I define
> one World mixin in mixin1.rb and another in mixin2.rb. In order to require
> them from env.rb I add the support directory to $LOAD_PATH. Basically,
> requiring them works, but they are loaded for the second time later by
> Cucumber.
> I'm guessing that Ruby/Cucumber don't understand that the files have already
> been loaded due to their being referred to by different pathnames (by env.rb
> and Cucumber). Maybe Cucumber needs to be more intelligent and avoid
> reloading files?
>
> Thanks
> Arve
>
> Are you using File.expand_path? If so, doesn't ruby know to not include them
> a second time when Cucumber calls Kernel#require ?
>

Actually, I think ruby 1.9 does expand_path on everything before
loading, and therefore has better caching than 1.8 (I might be wrong).
But all the smarts are inside Kernel.require - as I mentioned in my
other reply, ruby code can't know whether Kernel.require will actually
load a file or not.

Aslak

Matt Wynne

unread,
Sep 8, 2010, 10:22:53 AM9/8/10
to cu...@googlegroups.com

On 8 Sep 2010, at 14:43, aslak hellesoy wrote:

> On Wed, Sep 8, 2010 at 3:35 PM, Matt Wynne <ma...@mattwynne.net> wrote:
>>
>> On 8 Sep 2010, at 12:42, Arve Knudsen wrote:
>>
>> I have attached a case which demonstrates my problem. Basically, I define
>> one World mixin in mixin1.rb and another in mixin2.rb. In order to require
>> them from env.rb I add the support directory to $LOAD_PATH. Basically,
>> requiring them works, but they are loaded for the second time later by
>> Cucumber.
>> I'm guessing that Ruby/Cucumber don't understand that the files have already
>> been loaded due to their being referred to by different pathnames (by env.rb
>> and Cucumber). Maybe Cucumber needs to be more intelligent and avoid
>> reloading files?
>>
>> Thanks
>> Arve
>>
>> Are you using File.expand_path? If so, doesn't ruby know to not include them
>> a second time when Cucumber calls Kernel#require ?
>>
>
> Actually, I think ruby 1.9 does expand_path on everything before
> loading, and therefore has better caching than 1.8 (I might be wrong).
> But all the smarts are inside Kernel.require - as I mentioned in my
> other reply, ruby code can't know whether Kernel.require will actually
> load a file or not.

But the ruby code should be able to assume the file won't be loaded twice, as long as you give Kernel#require an expanded path, right?

Arve, maybe you're adding an un-necesary slash in there? Or maybe Cucumber isn't expanding paths properly?

Arve, if you run cucumber with -v you'll see it dump out all the files it loads. Try that, along with a puts statement in your env.rb to show the path being required by your code.

Arve Knudsen

unread,
Sep 8, 2010, 10:40:39 AM9/8/10
to cu...@googlegroups.com
On Wed, Sep 8, 2010 at 4:22 PM, Matt Wynne <ma...@mattwynne.net> wrote:

On 8 Sep 2010, at 14:43, aslak hellesoy wrote:

> On Wed, Sep 8, 2010 at 3:35 PM, Matt Wynne <ma...@mattwynne.net> wrote:
>>
>> On 8 Sep 2010, at 12:42, Arve Knudsen wrote:
>>
>> I have attached a case which demonstrates my problem. Basically, I define
>> one World mixin in mixin1.rb and another in mixin2.rb. In order to require
>> them from env.rb I add the support directory to $LOAD_PATH. Basically,
>> requiring them works, but they are loaded for the second time later by
>> Cucumber.
>> I'm guessing that Ruby/Cucumber don't understand that the files have already
>> been loaded due to their being referred to by different pathnames (by env.rb
>> and Cucumber). Maybe Cucumber needs to be more intelligent and avoid
>> reloading files?
>>
>> Thanks
>> Arve
>>
>> Are you using File.expand_path? If so, doesn't ruby know to not include them
>> a second time when Cucumber calls Kernel#require ?
>>
>
> Actually, I think ruby 1.9 does expand_path on everything before
> loading, and therefore has better caching than 1.8 (I might be wrong).
> But all the smarts are inside Kernel.require - as I mentioned in my
> other reply, ruby code can't know whether Kernel.require will actually
> load a file or not.

Arve, maybe you're adding an un-necesary slash in there? Or maybe Cucumber isn't expanding paths properly?

Well, as I said, if I require absolute paths the files don't get re-loaded, so on some level this must make Ruby see that Cucumber is requiring the same files (and reuse them). What doesn't work is putting the directory in $LOAD_PATH: "$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))" and requiring each file only by name (f.e.x 'mixin1').

Arve, if you run cucumber with -v you'll see it dump out all the files it loads. Try that, along with a puts statement in your env.rb to show the path being required by your code.

Thanks, this was helpful for diagnostics. Cucumber says for instance: "* features/support/mixin1.rb" before that file is loaded.

Arve

aslak hellesoy

unread,
Sep 8, 2010, 11:05:44 AM9/8/10
to cu...@googlegroups.com
On Wed, Sep 8, 2010 at 4:22 PM, Matt Wynne <ma...@mattwynne.net> wrote:
>
> On 8 Sep 2010, at 14:43, aslak hellesoy wrote:
>
>> On Wed, Sep 8, 2010 at 3:35 PM, Matt Wynne <ma...@mattwynne.net> wrote:
>>>
>>> On 8 Sep 2010, at 12:42, Arve Knudsen wrote:
>>>
>>> I have attached a case which demonstrates my problem. Basically, I define
>>> one World mixin in mixin1.rb and another in mixin2.rb. In order to require
>>> them from env.rb I add the support directory to $LOAD_PATH. Basically,
>>> requiring them works, but they are loaded for the second time later by
>>> Cucumber.
>>> I'm guessing that Ruby/Cucumber don't understand that the files have already
>>> been loaded due to their being referred to by different pathnames (by env.rb
>>> and Cucumber). Maybe Cucumber needs to be more intelligent and avoid
>>> reloading files?
>>>
>>> Thanks
>>> Arve
>>>
>>> Are you using File.expand_path? If so, doesn't ruby know to not include them
>>> a second time when Cucumber calls Kernel#require ?
>>>
>>
>> Actually, I think ruby 1.9 does expand_path on everything before
>> loading, and therefore has better caching than 1.8 (I might be wrong).
>> But all the smarts are inside Kernel.require - as I mentioned in my
>> other reply, ruby code can't know whether Kernel.require will actually
>> load a file or not.
>
> But the ruby code should be able to assume the file won't be loaded twice, as long as you give Kernel#require an expanded path, right?
>

1.9 will, but 1.8 won't.

Matt Wynne

unread,
Sep 8, 2010, 12:10:46 PM9/8/10
to cu...@googlegroups.com

Okay, so there really isn't anything Cucumber can do here, unless we added a 'don't auto-load' command-line switch, which seems like a bit of an edge case.

Arve it sounds like your workaround is either to require the files fully, without modifying $LOAD_PATH, or to start using Ruby 1.9. Are you happy with that?

Arve Knudsen

unread,
Sep 8, 2010, 1:06:47 PM9/8/10
to cu...@googlegroups.com
I would love to use Ruby 1.9 actually, but I had a real showstopper bug with that on Windows (spawned processes hang), that I've never got any response on. I could try again, possibly bug Ruby/RubyInstaller developers once more.

Arve

aslak hellesoy

unread,
Sep 8, 2010, 6:38:33 PM9/8/10
to cu...@googlegroups.com

Did you miss my suggestion further up in this thread? Just put those
files outside features and require them explicitly. Then they won't
get autoloaded by Cucumber.

Aslak

Arve Knudsen

unread,
Sep 9, 2010, 3:54:09 AM9/9/10
to cu...@googlegroups.com
I did notice your suggestion, I'll keep it in mind as an alternative strategy.

Arve
Reply all
Reply to author
Forward
0 new messages