Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reusable.rb « resource « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 24b0a1f6bce9b21394613a7d84812a6c31266f36 (plain)
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
# frozen_string_literal: true

module QA
  module Resource
    #
    # This module includes methods that allow resource classes to be reused safely. It should be prepended to a new
    # reusable version of an existing resource class. See Resource::Project and ReusableResource::Project for an example
    #
    module Reusable
      attr_accessor :reuse,
                    :reuse_as

      ResourceReuseError = Class.new(RuntimeError)

      def self.prepended(base)
        base.extend(ClassMethods)
      end

      # Gets an existing resource if it exists and the parameters of the new specification of the resource are valid.
      # Creates a new instance of the resource if it does not exist.
      #
      # @return [String] The URL of the resource.
      def fabricate_via_api!
        validate_reuse_preconditions

        resource_web_url(api_get)
      rescue Errors::ResourceNotFoundError
        super
      ensure
        self.class.resources[reuse_as] = self
      end

      # Including classes must confirm that the resource can be reused as defined. For example, a project can't be
      # fabricated with a unique name.
      #
      # @return [nil]
      def validate_reuse_preconditions
        return super if defined?(super)

        raise NotImplementedError
      end

      module ClassMethods
        # Removes all created resources of this type.
        #
        # @return [Hash<Symbol, QA::Resource>] the resources that were to be removed.
        def remove_all_via_api!
          resources.each do |reuse_as, resource|
            QA::Runtime::Logger.debug("#{self.name} - removing #{reuse_as}")
            resource.method(:remove_via_api!).super_method.call
          end
        end

        # The resources created by this resource class.
        #
        # @return [Hash<Symbol, QA::Resource>] the resources created by this resource class.
        def resources
          @resources ||= {}
        end
      end
    end
  end
end