unit-test ARGV problem (only 1 of 3 tests is using it)

Discussion in 'Ruby' started by ChrisKaelin, Apr 13, 2007.

  1. ChrisKaelin

    ChrisKaelin Guest

    I have 3 tests using the following setup routine, that is faking
    command line arguments via ARGV.
    Only one of my 3 test routines seems to be actually using the ARGV
    defined here (but the @run1 seems to be initialized, so I know at
    least, the setup routine is called by each test). Am I missing
    anything? I thought the setup routine is called for every test?

    def setup
    # faking commandline arguments first
    # no space here or the space will be
    # part of the filenames ;-)
    if RUBY_PLATFORM =~ /mswin32/
    #ARGV[1] = "-h"
    ARGV[1] = "-ltestlogfile"
    ARGV[2] = "-cdefault_config.yml"
    ARGV[3] = "-pC:\\TEMP"
    ARGV[4] = "-L4"
    ARGV[5] = "-v"
    else
    ARGV[1] = "-ltestlogfile"
    ARGV[2] = "-cdefault_config.yml"
    ARGV[3] = "-p\/tmp"
    end
    @run1 = TheScript.new
    @run1.set_vars
    end
     
    ChrisKaelin, Apr 13, 2007
    #1
    1. Advertising

  2. ChrisKaelin

    ChrisKaelin Guest

    So far I can only say, it has nothing to do with the environment I
    use. Also it does not matter wether I give the ARGV values in the
    setup routine or give it to the test script directly by commandline...
     
    ChrisKaelin, Apr 13, 2007
    #2
    1. Advertising

  3. ChrisKaelin

    ChrisKaelin Guest

    Found it...
    I used
    opts.parse!(args)
    as from the example from rdoc of OptionParser. Instead I should have
    used:
    opts.parse(args)
    which does not pop the ARGV values.
     
    ChrisKaelin, Apr 13, 2007
    #3
  4. ChrisKaelin

    Ryan Davis Guest

    On Apr 13, 2007, at 09:10 , ChrisKaelin wrote:

    > I have 3 tests using the following setup routine, that is faking
    > command line arguments via ARGV.
    > Only one of my 3 test routines seems to be actually using the ARGV
    > defined here (but the @run1 seems to be initialized, so I know at
    > least, the setup routine is called by each test). Am I missing
    > anything? I thought the setup routine is called for every test?


    setup IS called for every test.

    > def setup
    > # faking commandline arguments first
    > # no space here or the space will be
    > # part of the filenames ;-)
    > if RUBY_PLATFORM =~ /mswin32/
    > #ARGV[1] = "-h"
    > ARGV[1] = "-ltestlogfile"
    > ARGV[2] = "-cdefault_config.yml"
    > ARGV[3] = "-pC:\\TEMP"
    > ARGV[4] = "-L4"
    > ARGV[5] = "-v"
    > else
    > ARGV[1] = "-ltestlogfile"
    > ARGV[2] = "-cdefault_config.yml"
    > ARGV[3] = "-p\/tmp"
    > end
    > @run1 = TheScript.new
    > @run1.set_vars
    > end


    Try this instead:

    def setup
    ARGV.clear
    ARGV.push("-ltestlogfile", "-cdefault_config.yml")
    if RUBY_PLATFORM =~ /mswin32 then
    ARGV.push("-pC:\\TEMP", "-L4", "-v")
    else
    ARGV.push("-p\/tmp")
    end
    end

    I think the main problem is that ARGV is zero based and you're not
    respecting that by manually placing the elements in.
     
    Ryan Davis, Apr 13, 2007
    #4
  5. ChrisKaelin

    Ryan Davis Guest

    On Apr 13, 2007, at 15:45 , ChrisKaelin wrote:

    > Found it...
    > I used
    > opts.parse!(args)
    > as from the example from rdoc of OptionParser. Instead I should have
    > used:
    > opts.parse(args)
    > which does not pop the ARGV values.


    Popping the args should be fine as long as you put them back. In
    fact, I'd say it is better because you can guarantee what you're
    dealing with. Start with ARGV.clear and put what you want in it each
    time through.
     
    Ryan Davis, Apr 13, 2007
    #5
  6. ChrisKaelin

    ChrisKaelin Guest

    On 14 Apr., 00:44, Ryan Davis <> wrote:
    > On Apr 13, 2007, at 09:10 , ChrisKaelin wrote:
    >
    > > I have 3 tests using the following setup routine, that is faking
    > > command line arguments via ARGV.
    > > Only one of my 3 test routines seems to be actually using the ARGV
    > > defined here (but the @run1 seems to be initialized, so I know at
    > > least, the setup routine is called by each test). Am I missing
    > > anything? I thought the setup routine is called for every test?

    >
    > setup IS called for every test.
    >
    >
    >
    > > def setup
    > > # faking commandline arguments first
    > > # no space here or the space will be
    > > # part of the filenames ;-)
    > > if RUBY_PLATFORM =~ /mswin32/
    > > #ARGV[1] = "-h"
    > > ARGV[1] = "-ltestlogfile"
    > > ARGV[2] = "-cdefault_config.yml"
    > > ARGV[3] = "-pC:\\TEMP"
    > > ARGV[4] = "-L4"
    > > ARGV[5] = "-v"
    > > else
    > > ARGV[1] = "-ltestlogfile"
    > > ARGV[2] = "-cdefault_config.yml"
    > > ARGV[3] = "-p\/tmp"
    > > end
    > > @run1 = TheScript.new
    > > @run1.set_vars
    > > end

    >
    > Try this instead:
    >
    > def setup
    > ARGV.clear
    > ARGV.push("-ltestlogfile", "-cdefault_config.yml")
    > if RUBY_PLATFORM =~ /mswin32 then
    > ARGV.push("-pC:\\TEMP", "-L4", "-v")
    > else
    > ARGV.push("-p\/tmp")
    > end
    > end
    >
    > I think the main problem is that ARGV is zero based and you're not
    > respecting that by manually placing the elements in.


    Yeah, probably that was why it only worked one round... I will try
    your solution, so I can go back to use the correct way of the options
    parse. Thanks a lot!
     
    ChrisKaelin, Apr 17, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rtilley

    test unit with ARGV input

    rtilley, Mar 23, 2006, in forum: Ruby
    Replies:
    3
    Views:
    136
    rtilley
    Mar 23, 2006
  2. Gunnar Gauslaa Bergem
    Replies:
    0
    Views:
    120
    Gunnar Gauslaa Bergem
    Sep 5, 2006
  3. yuricake
    Replies:
    1
    Views:
    160
    Jano Svitok
    Apr 17, 2007
  4. Oliver Peng
    Replies:
    2
    Views:
    220
    Roger Pack
    Jul 28, 2010
  5. timr
    Replies:
    2
    Views:
    171
Loading...

Share This Page