|
| 1 | +package framework |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/DataDog/ddtest/internal/ext" |
| 10 | + "github.com/DataDog/ddtest/internal/testoptimization" |
| 11 | +) |
| 12 | + |
| 13 | +type Minitest struct { |
| 14 | + executor ext.CommandExecutor |
| 15 | +} |
| 16 | + |
| 17 | +func NewMinitest() *Minitest { |
| 18 | + return &Minitest{ |
| 19 | + executor: &ext.DefaultCommandExecutor{}, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func (m *Minitest) Name() string { |
| 24 | + return "minitest" |
| 25 | +} |
| 26 | + |
| 27 | +func (m *Minitest) DiscoverTests() ([]testoptimization.Test, error) { |
| 28 | + cleanupDiscoveryFile(TestsDiscoveryFilePath) |
| 29 | + |
| 30 | + cmd := m.createDiscoveryCommand() |
| 31 | + _, err := executeDiscoveryCommand(m.executor, cmd, m.Name()) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + tests, err := parseDiscoveryFile(TestsDiscoveryFilePath) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + slog.Debug("Parsed Minitest report", "tests", len(tests)) |
| 42 | + return tests, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (m *Minitest) RunTests(testFiles []string, envMap map[string]string) error { |
| 46 | + command, args, isRails := m.getMinitestCommand() |
| 47 | + |
| 48 | + // Add test files if provided |
| 49 | + if len(testFiles) > 0 { |
| 50 | + if isRails { |
| 51 | + // Rails test accepts files as command-line arguments |
| 52 | + args = append(args, testFiles...) |
| 53 | + } else { |
| 54 | + // Rake test requires TEST_FILES environment variable |
| 55 | + if envMap == nil { |
| 56 | + envMap = make(map[string]string) |
| 57 | + } |
| 58 | + envMap["TEST_FILES"] = strings.Join(testFiles, " ") |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // no-dd-sa:go-security/command-injection |
| 63 | + cmd := exec.Command(command, args...) |
| 64 | + |
| 65 | + applyEnvMap(cmd, envMap) |
| 66 | + |
| 67 | + return m.executor.Run(cmd) |
| 68 | +} |
| 69 | + |
| 70 | +// isRailsApplication determines if the current project is a Rails application |
| 71 | +func (m *Minitest) isRailsApplication() bool { |
| 72 | + // Check if rails gem is installed |
| 73 | + // no-dd-sa:go-security/command-injection |
| 74 | + cmd := exec.Command("bundle", "show", "rails") |
| 75 | + output, err := m.executor.CombinedOutput(cmd) |
| 76 | + if err != nil { |
| 77 | + slog.Debug("Not a Rails application: bundle show rails failed", "output", string(output), "error", err) |
| 78 | + return false |
| 79 | + } |
| 80 | + |
| 81 | + // Verify the output is a valid filepath that exists |
| 82 | + railsPath := strings.TrimSpace(string(output)) |
| 83 | + if railsPath == "" { |
| 84 | + slog.Debug("Not a Rails application: bundle show rails returned empty output") |
| 85 | + return false |
| 86 | + } |
| 87 | + if _, err := os.Stat(railsPath); err != nil { |
| 88 | + slog.Debug("Not a Rails application: rails gem path does not exist", "path", railsPath, "error", err) |
| 89 | + return false |
| 90 | + } |
| 91 | + |
| 92 | + // Check if rails command works |
| 93 | + // no-dd-sa:go-security/command-injection |
| 94 | + cmd = exec.Command("bundle", "exec", "rails", "version") |
| 95 | + output, err = m.executor.CombinedOutput(cmd) |
| 96 | + if err != nil { |
| 97 | + slog.Debug("Not a Rails application: bundle exec rails version failed", "output", string(output), "error", err) |
| 98 | + return false |
| 99 | + } |
| 100 | + |
| 101 | + // Verify the output starts with "Rails <version>" |
| 102 | + versionOutput := strings.TrimSpace(string(output)) |
| 103 | + if !strings.HasPrefix(versionOutput, "Rails ") { |
| 104 | + slog.Debug("Not a Rails application: rails version output does not start with 'Rails '", "output", versionOutput) |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + slog.Debug("Detected Rails application", "version_output", versionOutput) |
| 109 | + return true |
| 110 | +} |
| 111 | + |
| 112 | +// getMinitestCommand determines whether to use rails test or rake test |
| 113 | +// Returns: command, args, isRails |
| 114 | +func (m *Minitest) getMinitestCommand() (string, []string, bool) { |
| 115 | + isRails := m.isRailsApplication() |
| 116 | + if isRails { |
| 117 | + slog.Info("Found Ruby on Rails. Using bundle exec rails test for Minitest commands") |
| 118 | + return "bundle", []string{"exec", "rails", "test"}, true |
| 119 | + } |
| 120 | + |
| 121 | + slog.Info("No Ruby on Rails found. Using bundle exec rake test for Minitest commands") |
| 122 | + return "bundle", []string{"exec", "rake", "test"}, false |
| 123 | +} |
| 124 | + |
| 125 | +func (m *Minitest) createDiscoveryCommand() *exec.Cmd { |
| 126 | + command, args, _ := m.getMinitestCommand() |
| 127 | + |
| 128 | + // no-dd-sa:go-security/command-injection |
| 129 | + cmd := exec.Command(command, args...) |
| 130 | + cmd.Env = append( |
| 131 | + os.Environ(), |
| 132 | + "DD_TEST_OPTIMIZATION_DISCOVERY_ENABLED=1", |
| 133 | + "DD_TEST_OPTIMIZATION_DISCOVERY_FILE="+TestsDiscoveryFilePath, |
| 134 | + ) |
| 135 | + return cmd |
| 136 | +} |
0 commit comments