require 'spec_helper' describe Evrone::CI::Router::Travis do let(:content) { YAML.load fixture('travis.yml') } let(:travis) { described_class.from_attributes content } subject { travis } its(:attributes) { should be } its(:rvm) { should eq %w{ 2.0.0 } } its(:before_script) { should eq ["echo before_script"] } its(:script) { should eq ["RAILS_ENV=test ls -1 && echo DONE!"] } context "merge" do let(:new_attrs) { { rvm: "replaced" } } subject{ travis.merge new_attrs } it "should build a new travis instance" do expect(subject).to be_an_instance_of(described_class) end it "should replace attributes" do expect(subject.attributes["rvm"]).to eq %w{ replaced } end end context "(serialization)" do context "build new instance" do let(:expected) { { "rvm" => ["2.0.0"], "before_script" => ["echo before_script"], "script" => ["RAILS_ENV=test ls -1 && echo DONE!"], "env" => { "matrix" => [], "global" => [] } } } context "from_yaml" do subject { described_class.from_yaml(fixture('travis.yml')).attributes } it { should eq expected } end context "form_file" do subject { described_class.from_file('fixtures/travis.yml').attributes } it { should eq expected } end context "from_attributes" do let(:attrs) {{ rvm: "2.0.0", before_script: "echo before_script", script: "RAILS_ENV=test ls -1 && echo DONE!" }} subject { described_class.from_attributes(attrs).attributes } it { should eq expected } end end context ".to_yaml" do subject { travis.to_yaml } it { should eq travis.attributes.to_yaml } end end context "to_script_builder" do subject { travis.to_script_builder } it { should be } end context "to_matrix_s" do subject { travis.to_matrix_s } it { should eq 'rvm:2.0.0' } context "when many items" do before do mock(travis).rvm { %w{ 1.9.3 2.0.0 } } mock(travis).scala { %w{ 2.10.1 } } end it { should eq "rvm:1.9.3, rvm:2.0.0, scala:2.10.1" } end end context "matrix_keys" do subject { travis.matrix_keys } it { should eq %w{ rvm:2.0.0 } } context "when many items" do before do mock(travis).rvm { %w{ 1.9.3 2.0.0 } } mock(travis).scala { %w{ 2.10.1 } } end it { should eq %w{ rvm:1.9.3 rvm:2.0.0 scala:2.10.1 }} end end it "empty attributes must be empty Array" do expect(travis.scala).to eq([]) end context "normalize_attributes" do described_class::AS_ARRAY.each do |m| context "convert #{m} attribute to Array" do let(:content) { { m => m } } subject { travis.__send__(m) } it { should eq([m]) } end end context "convert hash keys to strings" do let(:content) { { rvm: "rvm" } } subject { travis.attributes } it { should include("rvm" => %w{rvm}) } end context "build env hash" do subject { travis.attributes["env"] } context "from String" do let(:content) { { env: "FOO" } } it { should eq( "global" => [], "matrix" => %w{ FOO } ) } end context "from Array" do let(:content) { { env: %w{ FOO BAR } } } it { should eq( 'global' => [], 'matrix' => %w{ FOO BAR } ) } end context "from empty Hash" do let(:content) { { env: {} } } it { should eq( 'global' => [], 'matrix' => [] ) } end context "from Hash" do let(:content) { { env: { "global" => "1", 'matrix' => '2' } } } it { should eq( 'global' => %w{1}, 'matrix' => %w{2} ) } end end end context "env" do let(:content) { { env: env } } subject { travis.env } context "when attributes[env] is Array" do let(:env) { %w{ FOO=1 BAR=2 } } it { should eq("matrix"=>["FOO=1", "BAR=2"], "global"=>[]) } end context "when attributes[env] is Hash" do let(:env) { { "matrix" => %w{ BAZ=1 } } } it { should eq("matrix"=>["BAZ=1"], "global"=>[]) } end context "when attributes[env] is empty" do let(:env) { {} } it { should eq("matrix"=>[], "global"=>[]) } end end context "global_env" do let(:content) { { env: env } } subject { travis.global_env } context "when attributes[env] is Array" do let(:env) { %w{ FOO=1 } } it { should eq([]) } end context "when attributes[env] is Hash" do let(:env) { { "global" => %w{ FOO=1 } } } it { should eq %w{ FOO=1 } } end context "when attributes[env] is empty" do let(:env) { {} } it { should eq([]) } end end end