Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
2a260cc9
Unverified
Commit
2a260cc9
authored
10 months ago
by
Chris Truter
Committed by
GitHub
10 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Demonstrate nesting dynamic redefines (#43496)
parent
f61a0657
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
test/metabase/test.clj
+5
-9
5 additions, 9 deletions
test/metabase/test.clj
test/metabase/test/util/dynamic_redefs.clj
+3
-3
3 additions, 3 deletions
test/metabase/test/util/dynamic_redefs.clj
test/metabase/test/util_test.clj
+16
-0
16 additions, 0 deletions
test/metabase/test/util_test.clj
with
24 additions
and
12 deletions
test/metabase/test.clj
+
5
−
9
View file @
2a260cc9
...
...
@@ -318,7 +318,11 @@
with-test-drivers
]
[
schema-migrations-test.impl
with-temp-empty-app-db
])
with-temp-empty-app-db
]
[
tu.dr
dynamic-value
with-dynamic-redefs
])
;; Rename this instead of using `import-vars` to make it clear that it's related to `=?`
(
p/import-fn
hawk.approx/malli
malli=?
)
...
...
@@ -327,11 +331,3 @@
(
alter-meta!
#
'with-temp
update
:doc
str
"\n\n Note: by default, this will execute its body inside a transaction, making
it thread safe. If it is wrapped in a call to [[metabase.test/test-helpers-set-global-values!]], it will affect the
global state of the application database."
)
;; Cursive does not understand p/import-macro, so we just proxy this manually
(
defmacro
with-dynamic-redefs
"A thread-safe version of with-redefs. It only support functions, and adds a fair amount of overhead.
It works by replacing each original definition with a proxy the first time it is redefined.
This proxy uses a dynamic mapping to check whether the function is currently redefined."
[
bindings
&
body
]
`
(
tu.dr/with-dynamic-redefs
~
bindings
~@
body
))
This diff is collapsed.
Click to expand it.
test/metabase/test/util/dynamic_redefs.clj
+
3
−
3
View file @
2a260cc9
...
...
@@ -8,8 +8,8 @@
"A thread-local mapping from vars to their most recently bound definition."
{})
(
defn
-
get-local-definition
"Get the v
ersion
of this
function
that is in scope. It is the unpatched version if there is no override."
(
defn
dynamic-value
"Get the v
alue
of this
var
that is in scope. It is the unpatched version if there is no override."
[
a-var
]
(
get
*local-redefs*
a-var
(
get
(
meta
a-var
)
::original
)))
...
...
@@ -18,7 +18,7 @@
"Build a proxy function to intercept the given var. The proxy checks the current scope for what to call."
[
a-var
]
(
fn
[
&
args
]
(
let
[
current-f
(
get-local-definition
a-var
)]
(
let
[
current-f
(
dynamic-value
a-var
)]
(
apply
current-f
args
))))
(
defn
patch-vars!
...
...
This diff is collapsed.
Click to expand it.
test/metabase/test/util_test.clj
+
16
−
0
View file @
2a260cc9
...
...
@@ -102,3 +102,19 @@
(
take-latch
)
(
testing
"The original definition survives"
(
is
(
=
"original"
(
clump
"orig"
"inal"
)))))))
(
defn
mock-me-inner
[]
:mock/original
)
(
defn
mock-me-outer
[]
(
mock-me-inner
))
(
deftest
with-dynamic-redefs-nested-binding-test
(
defn
z
[]
(
mt/with-dynamic-redefs
[
mock-me-outer
(
let
[
orig
(
mt/dynamic-value
mock-me-outer
)]
(
fn
[]
(
mt/with-dynamic-redefs
[
mock-me-inner
(
constantly
:mock/redefined
)]
(
orig
))))]
(
mock-me-outer
)))
(
is
(
=
:mock/redefined
(
z
))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment