From 9ee433eb2b3c604bd08b252e295e32b809081e66 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Mon, 18 Sep 2023 17:38:54 +0200 Subject: [PATCH] add more default patterns --- spec/lib/patterns_spec.cr | 77 ++++++++++++++++++++++++++++++++++++++- src/fingers/config.cr | 5 +++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/spec/lib/patterns_spec.cr b/spec/lib/patterns_spec.cr index 5281097..5fbee0e 100644 --- a/spec/lib/patterns_spec.cr +++ b/spec/lib/patterns_spec.cr @@ -4,7 +4,7 @@ require "string_scanner" def matches_for(pattern_name, input) pattern = Regex.new(::Fingers::Config::DEFAULT_PATTERNS[pattern_name]) - input.scan(pattern).map { |m| m[0] } + input.scan(pattern).map { |m| m["capture"]? || m[0] } end describe "default patterns" do @@ -100,4 +100,79 @@ describe "default patterns" do matches_for("path", input).should eq expected end end + + describe "hex" do + it "should match hex numbers" do + input = " + hello 0xcafe + 0xcaca + 0xdeadbeef hehehe 0xCACA + " + + expected = ["0xcafe", "0xcaca", "0xdeadbeef", "0xCACA"] + + matches_for("hex", input).should eq expected + end + end + + describe "git status" do + it "should match relevant stuff in git status output" do + input = " +Your branch is up to date with 'origin/crystal-rewrite'. + +Changes to be committed: + (use \"git restore --staged ...\" to unstage) + deleted: CHANGELOG.md + new file: wat + +Changes not staged for commit: + (use \"git add ...\" to update what will be committed) + (use \"git restore ...\" to discard changes in working directory) + modified: Makefile + modified: spec/lib/patterns_spec.cr + modified: src/fingers/config.cr + " + + expected = ["CHANGELOG.md", "wat", "Makefile", "spec/lib/patterns_spec.cr", "src/fingers/config.cr"] + + matches_for("git-status", input).should eq expected + end + end + + describe "git status branch" do + it "should match branch in git status output" do + input = " +Your branch is up to date with 'origin/crystal-rewrite'. + +Changes to be committed: + (use \"git restore --staged ...\" to unstage) + deleted: CHANGELOG.md + new file: wat + +Changes not staged for commit: + (use \"git add ...\" to update what will be committed) + (use \"git restore ...\" to discard changes in working directory) + modified: Makefile + modified: spec/lib/patterns_spec.cr + modified: src/fingers/config.cr + " + + expected = ["origin/crystal-rewrite"] + + matches_for("git-status-branch", input).should eq expected + end + end + + describe "git diff" do + it "should match a/b paths in git diff" do + input = " + diff --git a/spec/lib/patterns_spec.cr b/spec/lib/patterns_spec.cr + index 5281097..6c9c18e 100644 + --- a/spec/lib/patterns_spec.cr + +++ b/spec/lib/patterns_spec.cr + " + expected = ["spec/lib/patterns_spec.cr", "spec/lib/patterns_spec.cr"] + matches_for("diff", input).should eq expected + end + end end diff --git a/src/fingers/config.cr b/src/fingers/config.cr index 0d245c9..9cd83d9 100644 --- a/src/fingers/config.cr +++ b/src/fingers/config.cr @@ -30,6 +30,11 @@ module Fingers "digit": "[0-9]{4,}", "url": "((https?://|git@|git://|ssh://|ftp://|file:///)[^\\s()\"]+)", "path": "(([.\\w\\-~\\$@]+)?(/[.\\w\\-@]+)+/?)", + "hex": "(0x[0-9a-fA-F]+)", + "kubernetes": "(deployment.app|binding|componentstatuse|configmap|endpoint|event|limitrange|namespace|node|persistentvolumeclaim|persistentvolume|pod|podtemplate|replicationcontroller|resourcequota|secret|serviceaccount|service|mutatingwebhookconfiguration.admissionregistration.k8s.io|validatingwebhookconfiguration.admissionregistration.k8s.io|customresourcedefinition.apiextension.k8s.io|apiservice.apiregistration.k8s.io|controllerrevision.apps|daemonset.apps|deployment.apps|replicaset.apps|statefulset.apps|tokenreview.authentication.k8s.io|localsubjectaccessreview.authorization.k8s.io|selfsubjectaccessreviews.authorization.k8s.io|selfsubjectrulesreview.authorization.k8s.io|subjectaccessreview.authorization.k8s.io|horizontalpodautoscaler.autoscaling|cronjob.batch|job.batch|certificatesigningrequest.certificates.k8s.io|events.events.k8s.io|daemonset.extensions|deployment.extensions|ingress.extensions|networkpolicies.extensions|podsecuritypolicies.extensions|replicaset.extensions|networkpolicie.networking.k8s.io|poddisruptionbudget.policy|clusterrolebinding.rbac.authorization.k8s.io|clusterrole.rbac.authorization.k8s.io|rolebinding.rbac.authorization.k8s.io|role.rbac.authorization.k8s.io|storageclasse.storage.k8s.io)[[:alnum:]_#$%&+=/@-]+", + "git-status": "(modified|deleted|new file): +(?.+)", + "git-status-branch": "Your branch is up to date with '(?.*)'.", + "diff": "(---|\\+\\+\\+) [ab]/(?.*)", } ALPHABET_MAP = {