Class: Mesa

Inherits:
Object
  • Object
show all
Defined in:
lib/mesa_test.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mesa_dir: , mirror_dir: nil, github_protocol: :ssh) ⇒ Mesa

Returns a new instance of Mesa.



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/mesa_test.rb', line 566

def initialize(mesa_dir: ENV['MESA_DIR'], mirror_dir: nil,
               github_protocol: :ssh)
  # absolute_path ensures that it doesn't matter where commands are executed
  # from
  @mesa_dir = File.absolute_path(mesa_dir)
  @mirror_dir = File.absolute_path(mirror_dir)

  # don't worry about validity of github protocol until it is needed in a
  # checkout. This way you can have garbage in there if you never really need
  # it.
  @github_protocol = if github_protocol.respond_to? :to_sym
                       github_protocol.to_sym
                     else
                       github_protocol
                     end

  # these get populated by calling #load_test_data
  @test_cases = {}
  @test_case_names = {}
  @names_to_numbers = {}

  # way to output colored text
  @shell = Thor::Shell::Color.new
end

Instance Attribute Details

#github_protocolObject (readonly)

Returns the value of attribute github_protocol.



555
556
557
# File 'lib/mesa_test.rb', line 555

def github_protocol
  @github_protocol
end

#mesa_dirObject (readonly)

Returns the value of attribute mesa_dir.



555
556
557
# File 'lib/mesa_test.rb', line 555

def mesa_dir
  @mesa_dir
end

#mirror_dirObject (readonly)

Returns the value of attribute mirror_dir.



555
556
557
# File 'lib/mesa_test.rb', line 555

def mirror_dir
  @mirror_dir
end

#names_to_numbersObject (readonly)

Returns the value of attribute names_to_numbers.



555
556
557
# File 'lib/mesa_test.rb', line 555

def names_to_numbers
  @names_to_numbers
end

#shellObject (readonly)

Returns the value of attribute shell.



555
556
557
# File 'lib/mesa_test.rb', line 555

def shell
  @shell
end

#test_case_namesObject (readonly)

Returns the value of attribute test_case_names.



555
556
557
# File 'lib/mesa_test.rb', line 555

def test_case_names
  @test_case_names
end

#test_casesObject (readonly)

Returns the value of attribute test_cases.



555
556
557
# File 'lib/mesa_test.rb', line 555

def test_cases
  @test_cases
end

Class Method Details

.checkout(sha: nil, work_dir: nil, mirror_dir: nil, github_protocol: :ssh) ⇒ Object



558
559
560
561
562
563
564
# File 'lib/mesa_test.rb', line 558

def self.checkout(sha: nil, work_dir: nil, mirror_dir: nil,
                  github_protocol: :ssh)
  m = Mesa.new(mesa_dir: work_dir, mirror_dir: mirror_dir,
               github_protocol: github_protocol)   
  m.checkout(new_sha: sha)
  m
end

Instance Method Details

#build_log_64Object

base 64-encoded contents of build.log



734
735
736
737
738
739
# File 'lib/mesa_test.rb', line 734

def build_log_64
  build_log = File.join(mesa_dir, 'build.log')
  return '' unless File.exist?(build_log)

  b64_file(build_log)
end

#check_installationObject

throw an error unless it seems like it’s properly compiled

Raises:



726
727
728
729
730
731
# File 'lib/mesa_test.rb', line 726

def check_installation
  return if installed?

  raise MesaDirError, 'Installation check failed (build.log doesn\'t '\
                      'show a successful installation).'
end

#check_mod(mod) ⇒ Object

TEST SUITE METHODS

Raises:



760
761
762
763
764
765
# File 'lib/mesa_test.rb', line 760

def check_mod(mod)
  return if MesaTestCase.modules.include? mod

  raise TestCaseDirError, "Invalid module: #{mod}. Must be one of: " +
                          MesaTestCase.modules.join(', ')
end

#checkout(new_sha: 'HEAD') ⇒ Object

Raises:



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/mesa_test.rb', line 591

def checkout(new_sha: 'HEAD')
  # before anything confirm that git-lfs has been installed
  shell.say "\nEnsuring that git-lfs is installed... ", :blue
  command = 'git lfs help >> /dev/null 2>&1'
  if bash_execute(command)
    shell.say "yes", :green
  else
    shell.say "no", :red
    raise(GitHubError, "The command #{command} returned with an error "\
                       'status, indicating that git-lfs is not installed. '\
                       'Make sure it is installed and try again.')
  end

  # set up mirror if it doesn't exist
  unless dir_or_symlink_exists?(mirror_dir)
    shell.say "\nCreating initial mirror at #{mirror_dir}. "\
              'This might take awhile...', :blue
    FileUtils.mkdir_p mirror_dir
    case github_protocol
    when :ssh
      command = "git clone --mirror #{GITHUB_SSH} #{mirror_dir}"
      shell.say command
      # fail loudly if this doesn't work
      unless bash_execute(command)
        # nuke the mirror directory since it is probably bogus (make this
        # code fire off the next time checkout is done)
        shell.say "Failed. Removing the [potentially corrupted] mirror.", :red
        command = "rm -rf #{mirror_dir}"
        shell.say command
        bash_execute(command)

        raise(GitHubError, 'Error while executing the following command:'\
                           "#{command}. Perhaps you haven't set up "\
                           'ssh keys with your GitHub account?')
      end
    when :https
      command = "git clone --mirror #{GITHUB_HTTPS} #{mirror_dir}"
      shell.say command
      # fail loudly if this doesn't work
      unless bash_execute(command)
        # nuke the mirror directory since it is probably bogus (make this
        # code fire off the next time checkout is done)
        shell.say "Failed. Removing the [potentially corrupted] mirror.", :red
        command = "rm -rf #{mirror_dir}"
        shell.say command
        bash_execute(command)

        raise(GitHubError, 'Error while executing the following command: '\
                           "#{command}. Perhaps you need to configure "\
                           'global GitHub account settings for https '\
                           'authentication to work properly?')
      end
    else
      raise(GitHubError, "Invalid GitHub protocol: \"#{github_protocol}\"")
    end
  end

  # ensure "work" directory is removed from worktree
  remove

  update_mirror

  # create "work" directory with proper commit
  shell.say "\nSetting up worktree repo...", :blue
  FileUtils.mkdir_p mesa_dir
  command = "git -C #{mirror_dir} worktree add #{mesa_dir} #{new_sha}"
  shell.say command
  return if bash_execute(command)

  raise(GitHubError, 'Failed while executing the following command: '\
                     "\"#{command}\".")
end

#cleanObject



699
700
701
702
703
704
705
706
707
708
709
# File 'lib/mesa_test.rb', line 699

def clean
  with_mesa_dir do
    visit_and_check mesa_dir, MesaDirError, 'E\countered a problem in ' \
                              "running `clean` in #{mesa_dir}." do
      shell.say('MESA_DIR = ' + ENV['MESA_DIR'])
      shell.say './clean'
      bash_execute('./clean')
    end
  end
  self
end

#compiler_hashObject

sourced from $MESA_DIR/testhub.yml, which should be created after installation



743
744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/mesa_test.rb', line 743

def compiler_hash
  data_file = File.join(mesa_dir, 'testhub.yml')
  res = {
          compiler: 'Unknown',
          sdk_version: 'Unknown',
          math_backend: 'Unknown'
        }
  if File.exist? data_file
    res = res.merge(YAML.safe_load(File.read(data_file)) || {})
    # currently version_number is reported, but we don't need that in Git land
    res.delete('version_number') # returns the value, not the updated hash
    res
  end
end

#downloaded?Boolean

Returns:

  • (Boolean)


835
836
837
# File 'lib/mesa_test.rb', line 835

def downloaded?
  check_mesa_dir
end

#each_test_run(mod: :all) ⇒ Object



821
822
823
824
825
826
827
828
829
830
831
832
833
# File 'lib/mesa_test.rb', line 821

def each_test_run(mod: :all)
  check_installation

  if mod == :all
    MesaTestCase.modules.each do |this_mod|
      each_test_run(mod: this_mod)
    end
  else
    visit_dir(test_suite_dir(mod: mod)) do
      bash_execute('./each_test_run')
    end
  end
end

#find_test_case(test_case_name: nil, mod: :all) ⇒ Object

can accept a number (in string form) as a name for indexed access



813
814
815
816
817
818
819
# File 'lib/mesa_test.rb', line 813

def find_test_case(test_case_name: nil, mod: :all)
  if /\A[0-9]+\z/ =~ test_case_name
    find_test_case_by_number(test_number: test_case_name.to_i, mod: mod)
  else
    find_test_case_by_name(test_case_name: test_case_name, mod: mod)
  end
end

#git_shaObject



691
692
693
# File 'lib/mesa_test.rb', line 691

def git_sha
  bashticks("git -C #{mesa_dir} rev-parse HEAD")
end

#installObject



711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/mesa_test.rb', line 711

def install
  with_mesa_dir do
    visit_and_check mesa_dir, MesaDirError, 'Encountered a problem in ' \
                              "running `install` in #{mesa_dir}." do
      shell.say('MESA_DIR = ' + ENV['MESA_DIR'])
      shell.say './install'
      bash_execute('./install')
    end
  end
  # this should never happen if visit_and_check works properly.
  check_installation
  self
end

#install_attempted?Boolean

Returns:

  • (Boolean)


848
849
850
# File 'lib/mesa_test.rb', line 848

def install_attempted?
  File.exist? File.join(mesa_dir, 'testhub.yml')
end

#installed?Boolean

Returns:

  • (Boolean)


839
840
841
842
843
844
845
846
# File 'lib/mesa_test.rb', line 839

def installed?
  # assume build log reflects installation status; does not account for
  # mucking with modules after the fact
  build_log = File.join(mesa_dir, 'build.log')
  downloaded? && File.exist?(build_log) && File.read(build_log).include?(
    'MESA installation was successful'
  )
end

#load_test_source_data(mod: :all) ⇒ Object

load data from the ‘do1_test_source` file that gets used in a lot of testing



774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/mesa_test.rb', line 774

def load_test_source_data(mod: :all)
  # allow for brainless loading of all module data
  if mod == :all
    MesaTestCase.modules.each do |this_mod|
      load_test_source_data(mod: this_mod)
    end
  else
    check_mod mod

    # convert output of +list_tests+ to a dictionary that maps
    # names to numbers since +each_test_run+ only knows about numbers
    @names_to_numbers[mod] = {}
    @test_case_names[mod] = []
    @test_cases[mod] = {}
    visit_dir(test_suite_dir(mod: mod), quiet: true) do
      bashticks('./list_tests').split("\n").each do |line|
        num, tc_name = line.strip.split
        @names_to_numbers[mod][tc_name.strip] = num.to_i
        @test_case_names[mod] << tc_name.strip
        begin
          @test_cases[mod][tc_name.strip] = MesaTestCase.new(
            test: tc_name.strip,
            mod: mod,
            position: num.to_i,
            mesa: self
          )
        rescue TestCaseDirError
          shell.say "No such test case #{tc_name.strip}. Skipping loading it.", :red
        end
      end
    end
  end
end

#removeObject



675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/mesa_test.rb', line 675

def remove
  return unless File.exist? mesa_dir
  shell.say "\nRemoving work directory from worktree (clearing old data)...",
            :blue
  command = "git -C #{mirror_dir} worktree remove --force #{mesa_dir}"
  shell.say command
  return if bash_execute(command)

  shell.say "Failed. Simply trying to remove the directory.", :red
  command = "rm -rf #{mesa_dir}"
  shell.say command
  # fail loudly (the "true" tells bash_execute to raise an exception if
  # the command fails)
  bash_execute(command, true)
end

#shaObject



695
696
697
# File 'lib/mesa_test.rb', line 695

def sha
  git_sha
end

#test_case_count(mod: :all) ⇒ Object



808
809
810
# File 'lib/mesa_test.rb', line 808

def test_case_count(mod: :all)
  all_names_ordered(mod: mod).count
end

#test_suite_dir(mod: nil) ⇒ Object



767
768
769
770
# File 'lib/mesa_test.rb', line 767

def test_suite_dir(mod: nil)
  check_mod mod
  File.join(mesa_dir, mod.to_s, 'test_suite')
end

#update_mirrorObject

Raises:



664
665
666
667
668
669
670
671
672
673
# File 'lib/mesa_test.rb', line 664

def update_mirror
  shell.say "\nFetching MESA history...", :blue
  command = "git -C #{mirror_dir} fetch origin --prune"
  shell.say command
  # fail loudly
  return if bash_execute(command)

  raise(GitHubError, 'Failed while executing the following command: '\
                     "\"#{command}\".")
end

#with_mesa_dirObject

change MESA_DIR for the execution of the block and then revert to the original value



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'lib/mesa_test.rb', line 854

def with_mesa_dir
  # change MESA_DIR, holding on to old value
  orig_mesa_dir = ENV['MESA_DIR']
  ENV['MESA_DIR'] = mesa_dir
  shell.say "Temporarily changed MESA_DIR to #{ENV['MESA_DIR']}.", :blue

  # do the stuff
  begin
    yield
  # make sure we undo MESA_DIR change
  ensure
    ENV['MESA_DIR'] = orig_mesa_dir
    shell.say "Changed MESA_DIR back to #{ENV['MESA_DIR']}.", :blue
  end
end