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.



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/mesa_test.rb', line 525

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.



514
515
516
# File 'lib/mesa_test.rb', line 514

def github_protocol
  @github_protocol
end

#mesa_dirObject (readonly)

Returns the value of attribute mesa_dir.



514
515
516
# File 'lib/mesa_test.rb', line 514

def mesa_dir
  @mesa_dir
end

#mirror_dirObject (readonly)

Returns the value of attribute mirror_dir.



514
515
516
# File 'lib/mesa_test.rb', line 514

def mirror_dir
  @mirror_dir
end

#names_to_numbersObject (readonly)

Returns the value of attribute names_to_numbers.



514
515
516
# File 'lib/mesa_test.rb', line 514

def names_to_numbers
  @names_to_numbers
end

#shellObject (readonly)

Returns the value of attribute shell.



514
515
516
# File 'lib/mesa_test.rb', line 514

def shell
  @shell
end

#test_case_namesObject (readonly)

Returns the value of attribute test_case_names.



514
515
516
# File 'lib/mesa_test.rb', line 514

def test_case_names
  @test_case_names
end

#test_casesObject (readonly)

Returns the value of attribute test_cases.



514
515
516
# File 'lib/mesa_test.rb', line 514

def test_cases
  @test_cases
end

Class Method Details

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



517
518
519
520
521
522
523
# File 'lib/mesa_test.rb', line 517

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



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

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:



685
686
687
688
689
690
# File 'lib/mesa_test.rb', line 685

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:



719
720
721
722
723
724
# File 'lib/mesa_test.rb', line 719

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:



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
# File 'lib/mesa_test.rb', line 550

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

  update_mirror

  # ensure "work" directory is removed from worktree
  remove

  # 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



658
659
660
661
662
663
664
665
666
667
668
# File 'lib/mesa_test.rb', line 658

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



702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/mesa_test.rb', line 702

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)


794
795
796
# File 'lib/mesa_test.rb', line 794

def downloaded?
  check_mesa_dir
end

#each_test_run(mod: :all) ⇒ Object



780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/mesa_test.rb', line 780

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



772
773
774
775
776
777
778
# File 'lib/mesa_test.rb', line 772

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



650
651
652
# File 'lib/mesa_test.rb', line 650

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

#installObject



670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/mesa_test.rb', line 670

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)


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

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

#installed?Boolean

Returns:

  • (Boolean)


798
799
800
801
802
803
804
805
# File 'lib/mesa_test.rb', line 798

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



733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/mesa_test.rb', line 733

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



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/mesa_test.rb', line 634

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



654
655
656
# File 'lib/mesa_test.rb', line 654

def sha
  git_sha
end

#test_case_count(mod: :all) ⇒ Object



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

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

#test_suite_dir(mod: nil) ⇒ Object



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

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

#update_mirrorObject

Raises:



623
624
625
626
627
628
629
630
631
632
# File 'lib/mesa_test.rb', line 623

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

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