8000 Using minitest-line programmatically · Issue #19 · judofyr/minitest-line · GitHub
[go: up one dir, main page]

Skip to content

Using minitest-line programmatically #19

Open

Description

@alain-andre

Hi there,

I'm trying to programmatically run files at a specific line for my guard. I have created a stackoverflow question and I'm wondering if you could help me.

Activity

  1. grosser commented on Feb 5, 2018

    @grosser
    Contributor

    using ruby #{file}:#{line} would work for you ?
    ... if it does then adding support for multiple lines might work too ...
    or do you want to run the tests in the same process ? (spring-like)

  2. alain-andre commented on Feb 6, 2018

    @alain-andre
    Author

    Hi @grosser, I'm in a class inside my guardfile.

    What I need is when guard catch a modification, that it select the modified lines of the changed file and start the test at this lines.

    My Guardfile is like this

    class GitModifiedMatcher
      class MatchData
        def initialize(result)
          @result = result
        end
    
        def to_a
          @result.values
        end
    
        def [](name)
          @result[name]
        end
      end
    
      def match(filename_or_pathname)
        diff = `git diff --no-ext-diff --unified=0 --exit-code -a --no-prefix #{filename_or_pathname}`
        modified_lines = diff.to_enum(:scan, /^@@ -\d+,?.* [+](\d+),?.* @@.*/).map { Regexp.last_match(1) }
    
        return nil if modified_lines.count.zero?
        #Minitest::Test.tests_with_lines
        MatchData.new(file: filename_or_pathname)
      end
    end
    
    guard :minitest, all_on_start: false do
      watch(GitModifiedMatcher.new)
    end

    Instead of using Matchdata (MatchData.new(file: filename_or_pathname)), I would like to run a test for every lines in modified_lines on filename_or_pathname

  3. grosser commented on Feb 6, 2018

    @grosser
    Contributor
  4. alain-andre commented on Feb 7, 2018

    @alain-andre
    Author

    @grosser I need to run it in current process. I changed my code this way :

    class GitModifiedMatcher
      def match(file)
        diff = `git diff --no-ext-diff --unified=0 --exit-code -a --no-prefix #{file}`
        modified_lines = diff.to_enum(:scan, /^@@ -\d+,?.* [+](\d+),?.* @@.*/).map { Regexp.last_match(1) }
    
        modified_lines.each do |line|
          puts "Running #{file} at line #{line}"
          `ruby #{file} -l #{line}`
        end
      end
    end

    It fails because it does not find test_helper and then start to launch the whole tests of the file:

    10:17:39 - INFO - Guard::Minitest 2.4.6 is running, with Minitest::Unit 5.11.1!
    Running test/api/v01/plannings_test.rb at line 131
    test/api/v01/plannings_test.rb:1:in `require': cannot load such file -- test_helper (LoadError)
    	from test/api/v01/plannings_test.rb:1:in `<main>'
    10:18:19 - INFO - Running: test/api/v01/plannings_test.rb
4376 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

      0