Skip to content

Suggestions for testing options in Net::FTP.new #534

Closed

Description

@AndrewSpeed

I'm working on new specs for the options hash for new made available in Ruby 2.4 as part of #473, and I wanted to get some input on how to approach testing the ssl option, as this has a branching condition based on whether OpenSSL::SSL is defined. Do we have any examples in the codebase where something similar is being tested?

Apologies if this isn't the best place for this, I didn't see any methods of communication mentioned in contributing.md

Activity

  1. eregon commented on Oct 25, 2017

    @eregon
    Member

    Yes, this is a good place to ask :)

    I think in that case you can just require "openssl" in the examples (the its) that need it.
    It's better to not require other libraries at the top-level, as it can prevent loading other specs in the same file.

    If you want/need to test with openssl not available, you can use ruby_exe which starts a fresh new Ruby process.

  2. eregon commented on Oct 25, 2017

    @eregon
    Member

    Here is a somewhat similar example of YAML requiring OpenStruct in its specs:

    it "dumps an OpenStruct" do
    require "ostruct"
    os = OpenStruct.new("age" => 20, "name" => "John")
    YAML.dump(os).should match_yaml("--- !ruby/object:OpenStruct\ntable:\n :age: 20\n :name: John\n")
    end

  3. AndrewSpeed commented on Oct 25, 2017

    @AndrewSpeed
    ContributorAuthor

    Perfect, thanks very much @eregon I'll try implementing that tomorrow and hopefully get a PR up shortly afterwards 🙂

  4. AndrewSpeed commented on Oct 28, 2017

    @AndrewSpeed
    ContributorAuthor

    I'm having some difficulty testing a particular branch of the initialize method, namely: https://github.com/ruby/ruby/blob/trunk/lib/net/ftp.rb#L223

    The problem is that I want to test that when the ssl option is passed without openssl being imported, it should raise an exception. Normally the way to do this from what I've seen is to wrap the call you expect to raise the exception in a lambda, but for this I have to make use of ruby_exe as you suggested. Unfortunately this only seems to return string values, rather than raising any exceptions.

    I don't see any similar cases like this in the repository, do you have any suggestions on how best to approach this? One option could be to rescue the exception and then return it's string value from the ruby_exe call, but this doesn't feel right to me

  5. eregon commented on Oct 29, 2017

    @eregon
    Member

    One option could be to rescue the exception and then return it's string value from the ruby_exe call, but this doesn't feel right to me.

    That sounds like the simplest solution to me.

    ruby_exe("begin ... rescue => e; puts e.message; end").should == ...

    But actually, this should only happen if the openssl extension is not installed.
    That is, lib/net/ftp.rb does require "openssl" https://github.com/ruby/ruby/blob/5256f7752763249288662c6c5ffc5686d244a535/lib/net/ftp.rb#L23

    So I think it's actually not so useful to test for this condition, it's just a sign of a broken Ruby installation with no openssl.
    We generally assume the openssl extension is available in ruby/spec, it's part of the Ruby standard library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions