Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .codescene/code-health-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
{
"matching_content_path": "pluginkit/settings/parse.go",
"rules": [
{ "name": "String Heavy Function Arguments", "weight": 0.0 }
{ "name": "String Heavy Function Arguments", "weight": 0.0 },
{ "name": "Primitive Obsession", "weight": 0.0 }
]
}
]
Expand Down
9 changes: 7 additions & 2 deletions pluginkit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Releases of this module are tagged `pluginkit/vX.Y.Z`. Releases up to
- `Failure.Reason` is the plugin's own error with nothing in front, and a recovered panic reads `panicked: <value>`. `Failure.Err` keeps the whole text, so log lines read as before.
- A boot whose context ends pauses nothing, and a store read or write that fails is returned to the application.
- `pluginstoretest.Run` checks any `PluginStore` against the contract, and `pluginstoretest.NewMemory` is a store in memory that keeps it.
- `Installed` lists one `InstalledPlugin` per installed plugin, with the JSON names `id`, `name`, `description`, `version`, `status`, `paused_step` and `paused_reason`, sorted by name without regard to case, then by id. It leaves out a plugin the records hold as not installed and a record with no manifest, and lists a manifest with no record as active. The step and the reason are empty unless the plugin is paused.
- `Installed` lists one `InstalledPlugin` per installed plugin, with the JSON names `id`, `name`, `description`, `version`, `status`, `paused_step`, `paused_reason` and `has_settings`, sorted by name without regard to case, then by id. It leaves out a plugin the records hold as not installed and a record with no manifest, and lists a manifest with no record as active. The step and the reason are empty unless the plugin is paused, and `has_settings` is true when the manifest declares a setting.
- `wire.Manifest` reads `description`, which no longer sits in `Extra`, a breaking change.
- `wire.Manifest` reads `graphql` as `GraphQL`, which no longer sits in `Extra`, a breaking change.
- `wire` rejects a manifest that spells a key it reads in another case, such as `Name`, repeats a name in one object, or holds text that is not valid UTF-8, as bytes or as an escaped surrogate with no pair, a breaking change.
Expand All @@ -21,8 +21,13 @@ Releases of this module are tagged `pluginkit/vX.Y.Z`. Releases up to
- `RoutesEach` reads each plugin's routes and public paths inside a recover, and fails at the new `StepRoutes` a plugin whose `Routes` or `PublicPaths` panics, or whose `Routes` answers no handler.
- `settings.Check` checks the settings a plugin declares: seven types (`text`, `count`, `flag`, `duration`, `choice`, `secret`, `lines`), each key lowercase words joined by one underscore and outside the starts the application reserves, whole-number bounds on a count only, the options of a choice, a default for a count, a duration and a choice that its type accepts, a flag default of true or false, no default for a secret, and no `of`. A plugin with settings may not hold two hyphens in a row in its id or end it with one.
- `settings.Name` names the variable of each setting from the application's prefix, the plugin's id and the key.
- `Schema.Resolve` works out each value from the environment values the application found, then from the default. An empty value counts as unset, and a value its type rejects is named by its variable.
- `Schema.Resolve` takes `Sources`, the environment values by variable name and the saved values by key, and works out each value from the environment, then the saved value, then the default, setting its `Source`. A blank environment value counts as unset, for a secret too, and one its type rejects is named by its variable. A saved value its type no longer takes is reported as a `RejectedSavedValue` so the default applies, and a saved secret is never taken.
- A secret's value is a `settings.Hidden`, which prints, logs and encodes as `set` or `not set`.
- `settings.Field` keeps the `label` and `help` a manifest declares as `Label` and `Help`.
- `Schema.CheckChange` checks a change against the schema and the environment before it is saved, and answers its first problem as a `ChangeError` of the kind `unknown`, `locked` or `invalid`.
- A value its type rejects is a `settings.ValueError`, whose `Reason` is `not_whole`, `below_min`, `above_max`, `not_flag`, `not_duration`, `not_positive` or `not_option`, and whose text reads as before. A saved secret is reported with the reason `environment_only`.
- `Schema.FormFields` describes each field for a settings form as a `FormField`, in manifest order, and a secret only as set or not set.
- A duration `Value` keeps the text it was read from in `Text`, so `2h` stays `2h`.
- `ScreenGate` is an optional interface whose `ShowsScreens` tells an application whether a plugin's admin menu entries and screens show for the caller in the context.
- The module needs Go 1.27.4.

Expand Down
8 changes: 8 additions & 0 deletions pluginkit/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func TestSettingsDeclarations(t *testing.T) {
runFeature(t, "settings-declarations.feature", initializeSettings())
}

func TestSettingsSaved(t *testing.T) {
runFeature(t, "settings-saved.feature", initializeSettings())
}

func TestSettingsChanges(t *testing.T) {
runFeature(t, "settings-changes.feature", initializeSettings())
}

func TestScreenGate(t *testing.T) {
runFeature(t, "screen-gate.feature", initializeScreenGate(t))
}
20 changes: 15 additions & 5 deletions pluginkit/features/installed.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Feature: Listing the installed plugins
WordPress lists its installed plugins. The list joins the manifest of
each compiled plugin with the record the store keeps of it. Each row
holds the plugin's id, the name, description and version its manifest
gives, and the state its record holds. A paused row also holds the step
the plugin failed at and its own error, kept apart, so a screen can
build its sentence for the step from a translated template. A plugin
the application has not installed and a record whose plugin is no
longer compiled are left out.
gives, whether it declares any settings, and the state its record
holds. A paused row also holds the step the plugin failed at and its
own error, kept apart, so a screen can build its sentence for the step
from a translated template. A plugin the application has not installed
and a record whose plugin is no longer compiled are left out.

Scenario: An active plugin is listed with the name, description and version of its manifest
Given the compiled plugins "feed"
Expand Down Expand Up @@ -64,6 +64,16 @@ Feature: Listing the installed plugins
When the application lists the installed plugins
Then the list holds "feed" as "active" with no description and no version

Scenario: A plugin whose manifest declares a setting is listed with settings
Given the compiled plugins "events" and "feed"
And the manifest of "events" names it "Events"
And the manifest of "feed" names it "Feed"
And the manifest of "feed" declares the setting "title"
And the store holds no record
When the application lists the installed plugins
Then the list holds "feed" with settings
And the list holds "events" without settings

Scenario: The plugins are listed by name, then by id
Given the compiled plugins "events", "feed" and "gallery"
And the manifest of "events" names it "Calendar"
Expand Down
126 changes: 126 additions & 0 deletions pluginkit/features/settings-changes.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
Feature: Checking a change to a plugin's settings before it is saved
A downstream application lets an administrator change a plugin's
settings, and checks each change against the plugin's settings and the
environment before anything is saved. A change is accepted whole, or
rejected at its first problem with its kind, its key and why: a key the
plugin does not declare, a key the environment sets, a secret, which
comes from the environment only, or a value its type rejects. Keys the
plugin does not declare come first, sorted, then the declared fields in
the manifest's order. The application turns the kind and the reason into
its own answer.

Background:
Given the application names plugin variables under "APP_PLUGIN_"
And the plugin "notes" declares these settings:
| key | type | default | min | max | options |
| title | text | Notes | | | |
| per_page | count | 10 | 1 | 50 | |
| open | flag | false | | | |
| keep_for | duration | 720h | | | |
| order | choice | newest | | | newest, oldest |
| tags | lines | news | | | |
| api_key | secret | | | | |

Scenario: A change to declared settings is accepted
When a change to "notes" sets these settings:
| key | value |
| title | Field Notes |
| per_page | 25 |
| open | true |
| keep_for | 48h |
| order | oldest |
| tags | news, events |
Then the change is accepted

Scenario: A change to a key the plugin does not declare is rejected as unknown
When a change to "notes" sets "colour" to "green"
Then the change is rejected as "unknown" for "colour"

Scenario: A change to a field the environment sets is rejected as locked, naming its variable
Given the environment holds "Garden Notes" in "APP_PLUGIN_NOTES__TITLE"
When a change to "notes" sets "title" to "Field Notes"
Then the change is rejected as "locked" for "title"
And the rejection names the variable "APP_PLUGIN_NOTES__TITLE"

Scenario: A blank value in the environment locks nothing
Given the environment holds " " in "APP_PLUGIN_NOTES__TITLE"
When a change to "notes" sets "title" to "Field Notes"
Then the change is accepted

Scenario Outline: A value its type rejects is rejected as invalid with its reason
When a change to "notes" sets "<key>" to "<value>"
Then the change is rejected as "invalid" for "<key>" with the reason "<reason>"

Examples:
| key | value | reason |
| per_page | banana | not_whole |
| per_page | 2.5 | not_whole |
| per_page | 0 | below_min |
| per_page | 60 | above_max |
| open | maybe | not_flag |
| keep_for | soon | not_duration |
| keep_for | -1h | not_positive |
| order | random | not_option |

Scenario Outline: A count out of its bounds gives the bound its check used
When a change to "notes" sets "<key>" to "<value>"
Then the change is rejected as "invalid" for "<key>" with the reason "<reason>"
And the rejection gives only the <bound> <limit>

Examples:
| key | value | reason | bound | limit |
| per_page | 0 | below_min | min | 1 |
| per_page | 60 | above_max | max | 50 |

Scenario: A choice outside its options gives the options
When a change to "notes" sets "order" to "random"
Then the change is rejected as "invalid" for "order" with the reason "not_option"
And the rejection gives the options "newest, oldest"

Scenario: A change to a secret is rejected, because a secret comes from the environment only
When a change to "notes" sets "api_key" to "key-for-example"
Then the change is rejected as "invalid" for "api_key" with the reason "environment_only"
And the reason does not hold "key-for-example"

Scenario: A change to a secret the environment sets is rejected as locked
Given the environment holds "key-for-example" in "APP_PLUGIN_NOTES__API_KEY"
When a change to "notes" sets "api_key" to "another-key-for-example"
Then the change is rejected as "locked" for "api_key"
And the rejection names the variable "APP_PLUGIN_NOTES__API_KEY"
And the reason does not hold "key-for-example"

Scenario Outline: An empty value is accepted for a text or a lines field
When a change to "notes" sets "<key>" to ""
Then the change is accepted

Examples:
| key |
| title |
| tags |

Scenario Outline: An empty value is rejected for a type that has no empty value
When a change to "notes" sets "<key>" to ""
Then the change is rejected as "invalid" for "<key>" with the reason "<reason>"

Examples:
| key | reason |
| per_page | not_whole |
| open | not_flag |
| keep_for | not_duration |
| order | not_option |

Scenario: Keys the plugin does not declare are answered first, sorted
Given the environment holds "Garden Notes" in "APP_PLUGIN_NOTES__TITLE"
When a change to "notes" sets these settings:
| key | value |
| title | Field Notes |
| zebra | stripes |
| colour | green |
Then the change is rejected as "unknown" for "colour"

Scenario: The declared fields are answered in the manifest's order
When a change to "notes" sets these settings:
| key | value |
| order | random |
| per_page | banana |
Then the change is rejected as "invalid" for "per_page" with the reason "not_whole"
Loading