File: test_case.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (124 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true

ENV["MT_NO_PLUGINS"] = "yes, no plugins"
require "minitest"

require "byebug"
require "byebug/core"
require "byebug/interfaces/test_interface"
require_relative "utils"

module Byebug
  #
  # Extends Minitest's base test case and provides defaults for all tests.
  #
  class TestCase < Minitest::Test
    make_my_diffs_pretty!

    include TestUtils
    include Helpers::StringHelper

    def self.before_suite
      Byebug.init_file = ".byebug_test_rc"

      Context.interface = TestInterface.new
      Context.ignored_files = Context.all_files
    end

    #
    # Reset to default state before each test
    #
    def setup
      Byebug.start
      interface.clear
    end

    #
    # Cleanup temp files, and dummy classes/modules.
    #
    def teardown
      cleanup_namespace
      clear_example_file

      Byebug.breakpoints&.clear
      Byebug.catchpoints&.clear

      Byebug.stop
    end

    #
    # Removes test example file and its memoization
    #
    def clear_example_file
      example_file.close

      delete_example_file

      @example_file = nil
    end

    #
    # Cleanup main Byebug namespace from dummy test classes and modules
    #
    def cleanup_namespace
      force_remove_const(Byebug, example_class)
      force_remove_const(Byebug, example_module)
    end

    #
    # Temporary file where code for each test is saved
    #
    def example_file
      @example_file ||= File.new(example_path, "w+")
    end

    #
    # Path to file where test code is saved
    #
    def example_path
      File.join(example_folder, "byebug_example.rb")
    end

    #
    # Fully namespaced example class
    #
    def example_full_class
      "Byebug::#{example_class}"
    end

    #
    # Name of the temporary test class
    #
    def example_class
      "#{camelized_path}Class"
    end

    #
    # Name of the temporary test module
    #
    def example_module
      "#{camelized_path}Module"
    end

    #
    # Temporary folder where the test file lives
    #
    def example_folder
      @example_folder ||= File.realpath(Dir.tmpdir)
    end

    private

    def camelized_path
      camelize(File.basename(example_path, ".rb"))
    end

    def delete_example_file
      File.unlink(example_file)
    rescue StandardError
      # On windows we need the file closed before deleting it, and sometimes it
      # didn't have time to close yet. So retry until we can delete it.
      retry
    end
  end
end