lazy-check 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 763c1844791d4316d45e4d40b086e708ff10ec136c1aed5168bf1e1055b52200
4
+ data.tar.gz: 20c67417035d9daf100977c26a15075333a6cd9fc66ac38373bc66a2d8f6a01f
5
+ SHA512:
6
+ metadata.gz: d68d5aaaf638e3540780c23d778d1de189df696778d6c92e1ba6ec0d6f3cc894ff28032bbb0270dc4c81cf3dee4a13de3c47b787dc3da1ae431e847fd478574b
7
+ data.tar.gz: 3340e1c675e78edcd698c0a30206232add999f8061c60613a1703caf2256bf6bfec5c9e7894959bba159b3f099e15216bc542ab83165693ca70ab00e13a1a418
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.0.01
2
+
3
+ * Création du gem
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lazy-check.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lazy-check (0.1.0)
5
+ clir
6
+ nokogiri
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ clir (0.22.1)
12
+ json
13
+ tty-prompt
14
+ json (2.6.3)
15
+ mini_portile2 (2.8.4)
16
+ minitest (5.20.0)
17
+ minitest-color (0.0.2)
18
+ minitest (~> 5)
19
+ nokogiri (1.15.4)
20
+ mini_portile2 (~> 2.8.2)
21
+ racc (~> 1.4)
22
+ pastel (0.8.0)
23
+ tty-color (~> 0.5)
24
+ racc (1.7.1)
25
+ rake (12.3.3)
26
+ tty-color (0.6.0)
27
+ tty-cursor (0.7.1)
28
+ tty-prompt (0.23.1)
29
+ pastel (~> 0.8)
30
+ tty-reader (~> 0.8)
31
+ tty-reader (0.9.0)
32
+ tty-cursor (~> 0.7)
33
+ tty-screen (~> 0.8)
34
+ wisper (~> 2.0)
35
+ tty-screen (0.8.1)
36
+ wisper (2.0.1)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ lazy-check!
43
+ minitest (~> 5.0)
44
+ minitest-color
45
+ rake (~> 12.0)
46
+
47
+ BUNDLED WITH
48
+ 2.1.4
@@ -0,0 +1,39 @@
1
+ # Lazy::Checker, notes développeur
2
+
3
+ ## Fonctionnement général du check
4
+
5
+ L'utilisateur crée une instance `Lazy::Checker` (`checker.rb`) avec une adresse de recette (ou la recette présente dans le dossier où a été ouvert le Terminal)
6
+
7
+ L'utilisateur appelle alors la méthode `#check` de cette instance, avec ou sans options (pour le moment, les options permettent seulement de déterminer si on doit afficher le résultat ou le retourner.
8
+
9
+ La méthode `#check` vérifie si la recette est valide, et appelle le cas échéant la méthode `#proceed_check` avec les options.
10
+
11
+ La méthode `Lazy::Checker#proceed_check` crée un rapporteur (`Lazy::Checker::Reporter`), le démarre et boucle sur chaque test de la propriété `:tests de la recette` (un `Array`).
12
+
13
+ Pour chaque test, la méthode crée une instance `Lazy::Checker::Test` en lui donnant en argument l'instance checker (donc elle-même) et les données du test.
14
+
15
+ > Parmi ces données de test se trouve `:url` qui détermine l'adresse URI qu'il va falloir tester, ainsi que `:checks` qui détermine la liste des checks à effectuer.
16
+
17
+ Une fois tous les tests instanciés, on appelle leur méthode `#check`. Cette méthode boucle sur la propriété `:checks` des données du check, instancie un `Lazy::Checker::CheckCase` et appelle sa méthode `#check`.
18
+
19
+ La méthode `Lazy::Checker::CheckCase#check` crée une instance `Lazy::Checker::CheckedTag` à partir des données du cas (celles qui contiennent `:tag`, `:text`, `:contains`, etc.) et vérifie que ce « tag » appartienne bien au conteneur défini par l'URL à l'aide de la méthode `#is_in?` qui est, pourrait-on dire, le cœur de ce gem.
20
+
21
+ La méthode `#is_in?` retourne true ou false et crée respectivement un succès ou un échec dans le rapporteur.
22
+
23
+
24
+ ~~~yaml
25
+ # --- La recette --- #=> instance Lazy::Checker
26
+ name: Test d'une page internet
27
+ base: https:://mon_site.com # toutes les adresses sont vues par là
28
+ # Les tests
29
+ tests:
30
+ - name: Nom du premier test #=> instance Lazy::Checker::Test
31
+ url: relative/to/page
32
+ checks:
33
+ # :url + 1 :check => instance Lazy::Checker::CheckCase
34
+ - tag: div#mondiv.content #=> instance Lazy::Checker::CheckedTag
35
+ count: 1
36
+ text: "Bonjour tout le monde !"
37
+ - tag: div#autre_div #=> instance Lazy::Checker::CheckedTag
38
+
39
+
data/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # Lazy::Check
2
+
3
+ Ce gem permet de tester de façon paresseuse un site web.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lazy-check'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lazy-check
20
+
21
+ ## Usage
22
+
23
+ ### Pour un test simple
24
+
25
+ Si on a juste du code XML-like à tester, on peut utiliser la méthode `Lazy::Checker.check`.
26
+
27
+ ~~~ruby
28
+ require 'lazy-check'
29
+
30
+ code = "<root><div class="contient">du texte</div></root>"
31
+ check = {tag: 'div.contient', text: "du texte"}
32
+ Lazy::Checker.check(code, check)
33
+ # => Écrit :
34
+ # -------------------------------------
35
+ # Succès 1 Failure 0 Temps ...
36
+ ~~~
37
+
38
+ On peut aussi obtenir les résultats en retour de méthode (c'est un `Lazy::Checker::Reporter`)
39
+
40
+ ~~~ruby
41
+ Lazy::Checker.check(code, check, **{return_result: true})
42
+ # => Reporter
43
+ ~~~
44
+
45
+ > Noter que dans ce cas-là, rien n'est écrit en console.
46
+
47
+ ## Pour un test avec recette
48
+
49
+ Une « recette » est un fichier `YAML` qui définit l'url d'une page internet, ainsi que les checks à effectuer dessus. Cf. ci-dessous.
50
+
51
+ ~~~ruby
52
+ require "lazy-check"
53
+
54
+ checker = Lazy::Checker.new("path/to/recipe.yaml")
55
+ checker.check
56
+ # => Produit le résultat
57
+ ~~~
58
+
59
+ Si la recette se trouve là où le terminal se trouve, il suffit de faire :
60
+
61
+ ~~~ruby
62
+ require "lazy-check"
63
+
64
+ Lazy::Checker.new.check
65
+ ~~~
66
+
67
+ La recette (`recipe.yaml`) définit les vérifications qu'il faut effectuer.
68
+
69
+ ~~~yaml
70
+ ---
71
+ name: "Nom général de la recette"
72
+ base: https://www.mon.domaine.net
73
+ tests:
74
+ - name: "Le premier test"
75
+ url: "" # donc la base
76
+ checks:
77
+ - name: "Existence du DIV#content avec du texte"
78
+ tag: 'div#content'
79
+ empty: false
80
+ - name: "Existence du SPAN#range sans texte"
81
+ tag: 'span#range'
82
+ empty: true
83
+
84
+ - name: "Une redirection"
85
+ url: "redirection.html"
86
+ redirect_to: "https://nouvelle.url.net"
87
+
88
+ - name: "Une page erronée"
89
+ url: "page_inexistante.html"
90
+ response: 404
91
+ ~~~
92
+
93
+ ### Check Properties
94
+
95
+ ~~~yaml
96
+ tag: [String] Le sélector
97
+ count: [Integer] Nombre attendu d'éléments
98
+ empty: [Bool] Si true, doit être vide ou non vide
99
+ direct_child: [Bool] Si true, doit être un enfant direct
100
+ text: [String] Le texte qui doit être contenu
101
+ contains: [String|Array] Ce que doit contenir la page
102
+ min_length: [Integer] La longueur minimum du contenu (text seulement)
103
+ max_length: [Integer] La longueur maximum du contenu (text seulement)
104
+ ~~~
105
+
106
+ ## Exemples
107
+
108
+ Simplement vérifier qu’une page réponde correctement :
109
+
110
+ ~~~yaml
111
+ # recipe.yaml
112
+ ---
113
+ name: "La page existe"
114
+ base: 'https://monsite.net'
115
+ tests:
116
+ - name: "La page index.html existe et répond correctement"
117
+ url: 'index.html'
118
+ response: 200
119
+ ~~~
120
+
121
+ Vérifier qu’une page contient les éléments de base
122
+
123
+ ~~~yaml
124
+ # recipe.yaml
125
+ ---
126
+ name: "Check simple de l'existence des éléments de base"
127
+ base: 'https://monsite.net'
128
+ tests:
129
+ - name: "La page base.html contient les éléments de base"
130
+ url: 'index.html'
131
+ checks:
132
+ - tag: 'header'
133
+ - tag: 'section#body'
134
+ - tag: 'footer'
135
+ ~~~
136
+
137
+
138
+
139
+ ## Development
140
+
141
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
142
+
143
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
144
+
145
+ ## Contributing
146
+
147
+ Bug reports and pull requests are welcome on GitHub at https://github.com/PhilippePerret/lazy-check.
148
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lazy/check"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ require_relative 'lib/lazy/check/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "lazy-check"
5
+ s.version = Lazy::Check::VERSION
6
+ s.authors = ["PhilippePerret"]
7
+ s.email = ["philippe.perret@yahoo.fr"]
8
+
9
+ s.summary = %q{Vérification paresseuse d'un site web}
10
+ s.description = %q{Ce gem permet de façon paresseuse mais néanmoins sérieuse de tester qu'un site web est valide au niveau de ses pages et de son contenu.}
11
+ s.homepage = "https://rubygems.org/gems/lazy-check"
12
+ s.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ s.add_development_dependency 'minitest'
15
+ s.add_development_dependency 'minitest-color'
16
+
17
+ s.add_dependency 'clir'
18
+ s.add_dependency 'nokogiri'
19
+
20
+ s.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ s.metadata["homepage_uri"] = s.homepage
23
+ s.metadata["source_code_uri"] = "https://github.com/PhilippePerret/gem-lazy-check"
24
+ s.metadata["changelog_uri"] = "https://github.com/PhilippePerret/gem-lazy-check/CHANGELOG.md"
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|features)/}) }
30
+ end
31
+ s.bindir = "exe"
32
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ s.require_paths = ["lib"]
34
+ end
@@ -0,0 +1,147 @@
1
+ #
2
+ # Cf. code sur Github
3
+ # https://github.com/sparklemotion/nokogiri/blob/main/lib/nokogiri/xml/node.rb
4
+ #
5
+ module Nokogiri
6
+ class XML::Element
7
+
8
+ # Liste [Array] des erreurs rencontrées au cours d'un check
9
+ # Il faut penser initialiser (@errors = []) cette valeur au
10
+ # début d'un check
11
+ attr_reader :errors
12
+
13
+ def traverse_children(&block)
14
+ children.each { |ch| ch.traverse(&block) }
15
+ end
16
+
17
+ # @return true si le node est vraiment vide
18
+ def empty?
19
+ not(children?) && content.empty?
20
+ end
21
+
22
+ def children?
23
+ elements && elements.count > 0
24
+ end
25
+
26
+ def has_text?
27
+ not(text.empty?)
28
+ end
29
+
30
+ def has_no_text?
31
+ text.empty?
32
+ end
33
+
34
+ # Test du contenu
35
+ #
36
+ # @return true si le node courant contient tout ce qui est défini
37
+ # dans +required+
38
+ #
39
+ # @param required [String|Hash|Array] les choses qu'on doit
40
+ # trouver dans le noeud. Ça peut être un simple
41
+ # texte définissant soit un texte soit une balise,
42
+ # une table définissant une balise ou une liste
43
+ # de ces différentes choses.
44
+ def contains?(requireds)
45
+ @errors = []
46
+ @ok = true
47
+ requireds = [requireds] unless requireds.is_a?(Array)
48
+
49
+ #
50
+ # Recherche sur tous les éléments requis
51
+ #
52
+ # On ne s'arrête pas, même si une erreur a été trouvée, pour
53
+ # pouvoir les relever toutes.
54
+ #
55
+ requireds.each do |required|
56
+ #
57
+ # Qu'est-ce qu'on doit chercher ?
58
+ #
59
+ case required
60
+ when String
61
+ if required.match?(/[^ ]/) && required.match?(/[#.]/)
62
+ contains_as_tag?({tag: required})
63
+ else
64
+ contains_as_string?(required)
65
+ end
66
+ when Hash
67
+ contains_as_tag?(required)
68
+ else
69
+ # Erreur d'implémentation, je dois m'arrêter
70
+ raise CheckCaseError.new(Lazy::ERRORS[2000] % {c: required.class.name})
71
+ end
72
+ end
73
+
74
+ return @errors.empty? # true si OK
75
+ end
76
+ #/ contains?
77
+
78
+
79
+ # @return true si le node contient le texte +searched+
80
+ # (mais ce retour ne sert pas à grand-chose, en fait, et même
81
+ # à rien)
82
+ # Surtout : ajoute une erreur à @errors si une erreur est
83
+ # rencontrées.
84
+ #
85
+ # @note
86
+ # alias #match?
87
+ #
88
+ def contains_as_string?(searched)
89
+ if text.include?(searched)
90
+ return true
91
+ else
92
+ add_error(Lazy::ERRORS[5020] % {e: searched})
93
+ return false
94
+ end
95
+ end
96
+ alias :match? :contains_as_string?
97
+
98
+ # @return true si le node contient le node +dtag+ ou produit
99
+ # une erreur dans @errors
100
+ #
101
+ def contains_as_tag?(dtag)
102
+ ctag = Lazy::Checker::CheckedTag.new(dtag)
103
+ if ctag.is_in?(self)
104
+ return true
105
+ else
106
+ add_error(Lazy::ERRORS[5021] % {e: dtag.inspect})
107
+ return false
108
+ end
109
+ end
110
+
111
+ # @return [Array] Liste des attributs qui manque à l'élément
112
+ # par rapport à attrs
113
+ def attributes?(attrs)
114
+ miss_attrs = []
115
+ attrs.each do |attr_name, attr_value|
116
+ attr_name = attr_name.to_s
117
+ if self.key?(attr_name)
118
+ # L'attribut existe
119
+ if self.attr(attr_name) == attr_value
120
+ # … et sa valeur est la bonne
121
+ next
122
+ else
123
+ # … mais sa valeur est différente
124
+ miss_attrs << "attribut #{attr_name} existe, mais avec la valeur #{self.attr(attr_name).inspect}, pas #{attr_value.inspect}."
125
+ end
126
+ else
127
+ miss_attrs << "pas d'attribut #{attr_name.inspect}"
128
+ end
129
+ end
130
+ return miss_attrs
131
+ end
132
+
133
+ def add_error(err)
134
+ @errors ||= []
135
+ @errors << err
136
+ end
137
+
138
+ def id
139
+ @id ||= self['id']
140
+ end
141
+
142
+ # @return [Integer] La longueur du texte
143
+ def length
144
+ @length ||= text.length
145
+ end
146
+ end #/class XML::Element
147
+ end #/module Nokogiri