Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg.json: test coverage, reference impl #44

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ deps:
$(LUAROCKS) install inspect
$(LUAROCKS) install net-url
$(LUAROCKS) install jsonschema
$(LUAROCKS) install lua-cjson

.PHONY: test
test: deps json
./scripts/packspec.lua examples/packspec.1.lua
./scripts/packspec.lua examples/packspec.1.json
nvim -l ./scripts/packspec.lua examples/packspec.1.lua
nvim -l ./scripts/packspec.lua examples/packspec.1.json
./deps/bin/busted

.PHONY: json
json: deps
./scripts/generate_json_schema.lua
nvim -l ./scripts/generate_json_schema.lua

.PHONY: clean
clean:
Expand Down
4 changes: 2 additions & 2 deletions lua/packspec/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ end

return {
title = "packspec",
description = "A package specification for Neovim",
description = "Package format for declaring decentralized, ad-hoc dependencies",
type = 'object',
additionalProperties = false,
properties = {
package = {
description = "The name of the package",
description = "Name of the package (optional)",
type = "string",
},
version = {
Expand Down
3 changes: 1 addition & 2 deletions lua/utils/format_cjson.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
-- OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

local ok, cjson = pcall(require, "cjson.safe")
local enc = ok and cjson.encode or function() return nil, "Lua cJSON encoder not found" end
local enc = vim.json.encode
local cat = table.concat
local sub = string.sub
local rep = string.rep
Expand Down
156 changes: 78 additions & 78 deletions schema/packspec_schema.json
Original file line number Diff line number Diff line change
@@ -1,117 +1,117 @@
{
"type": "object",
"title": "packspec",
"required": [
"package",
"source"
],
"additionalProperties": false,
"properties": {
"packspec": {
"type": "string",
"description": "The current specification version. (0.1.0) at this time.",
"pattern": "^(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*$"
},
"dependencies": {
"patternProperties": {
".*": {
"type": "object",
"additionalProperties": false,
"properties": {
"releases_only": {
"description": "Whether the package manager should only resolve version\nconstraints to include tagged releases.",
"type": "boolean"
},
"version": {
"type": "string",
"description": "Version constraints on the package.\n * Accepted operators are the relational operators of Lua:\n == \\~= < > <= >= , as well as a special operator, \\~>,\n inspired by the \"pessimistic operator\" of RubyGems\n (\"\\~> 2\" means \">= 2, < 3\"; \"~> 2.4\" means \">= 2.4, < 2.5\").\n No operator means an implicit == (i.e., \"lfs 1.0\" is the\n same as \"lfs == 1.0\"). \"lua\" is an special dependency name;\n it matches not a rock, but the version of Lua in use.\n Multiple version constraints can be joined with a `comma`,\n e.g. `\"neovim >= 5.0, < 7.0\"`.\n * If no version is specified, then HEAD is assumed valid.\n * If no upper bound is specified, then any commit after the\n tag corresponding to the lower bound is assumed valid. The\n commit chosen is up to the plugin manager's discretion, but\n implementers are strongly encouraged to always use the\n latest valid commit.\n * If an upper bound is specified, then the the tag\n corresponding to that upper bound is the latest commit that\n is valid",
"pattern": "^((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*(\\s*,\\s*((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*)*$"
},
"source": {
"type": "string",
"description": "Source of the dependency. See previous `source` description.",
"pattern": "^\\w+:\/\/"
}
}
}
}
},
"version": {
"type": "string",
"description": "The version of the package. Should obey semantic versioning\nconventions, for example `0.1.0`. Plugins should have a git commit\nwith a `tag` matching this version. For all version identifiers,\nimplementation should check for a `version` prefixed with `v` in the\ngit repository, as this is a common convention.",
"pattern": "^(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*$"
},
"external_dependencies": {
"description": "Like dependencies, this specifies packages which are required for the\npackage but should *not* be managed by the Neovim package manager, such\nas `gcc` or `cmake`. Package managers are encouraged to provide a\nnotification to the user if the dependency is not available.",
"patternProperties": {
".*": {
"type": "object",
"additionalProperties": false,
"properties": {
"version": {
"type": "string",
"description": "Same as `dependencies`",
"pattern": "^((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*(\\s*,\\s*((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*)*$"
"pattern": "^((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*(\\s*,\\s*((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*)*$",
"type": "string"
}
}
},
"type": "object"
}
}
},
"description": "Like dependencies, this specifies packages which are required for the\npackage but should *not* be managed by the Neovim package manager, such\nas `gcc` or `cmake`. Package managers are encouraged to provide a\nnotification to the user if the dependency is not available."
},
"package": {
"description": "The name of the package",
"packspec": {
"description": "The current specification version. (0.1.0) at this time.",
"pattern": "^(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*$",
"type": "string"
},
"package": {
"type": "string",
"description": "The name of the package"
},
"description": {
"additionalProperties": false,
"description": "Description of the package",
"properties": {
"summary": {
"description": "Short description of the package, typically less than 100 character\nlong.",
"type": "string"
"type": "string",
"description": "Short description of the package, typically less than 100 character\nlong."
},
"homepage": {
"description": "Homepage of the package. In most cases this will be the GitHub URL.",
"type": "string"
"type": "string",
"description": "Homepage of the package. In most cases this will be the GitHub URL."
},
"detailed": {
"type": "string",
"description": "Long-form description of the package, this should convey the\npackage's principal functionality to the user without being as\ndetailed as the package readme."
},
"license": {
"type": "string",
"description": "This is [SPDX](https:\/\/spdx.org\/licenses\/) license identifier. Dual\nlicensing is indicated via joining the relevant licenses via `\/`."
},
"author": {
"additionalProperties": false,
"description": "Author of the package",
"properties": {
"email": {
"type": "string",
"description": "Author email"
},
"name": {
"description": "Author name",
"type": "string",
"description": "Author name"
}
},
"type": "object"
}
},
"type": "object"
},
"version": {
"description": "The version of the package. Should obey semantic versioning\nconventions, for example `0.1.0`. Plugins should have a git commit\nwith a `tag` matching this version. For all version identifiers,\nimplementation should check for a `version` prefixed with `v` in the\ngit repository, as this is a common convention.",
"pattern": "^(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*$",
"type": "string"
},
"$schema": {
"type": "string",
"description": "The optional json schema URI for validation with json-language-server."
},
"dependencies": {
"patternProperties": {
".*": {
"additionalProperties": false,
"properties": {
"version": {
"description": "Version constraints on the package.\n * Accepted operators are the relational operators of Lua:\n == \\~= < > <= >= , as well as a special operator, \\~>,\n inspired by the \"pessimistic operator\" of RubyGems\n (\"\\~> 2\" means \">= 2, < 3\"; \"~> 2.4\" means \">= 2.4, < 2.5\").\n No operator means an implicit == (i.e., \"lfs 1.0\" is the\n same as \"lfs == 1.0\"). \"lua\" is an special dependency name;\n it matches not a rock, but the version of Lua in use.\n Multiple version constraints can be joined with a `comma`,\n e.g. `\"neovim >= 5.0, < 7.0\"`.\n * If no version is specified, then HEAD is assumed valid.\n * If no upper bound is specified, then any commit after the\n tag corresponding to the lower bound is assumed valid. The\n commit chosen is up to the plugin manager's discretion, but\n implementers are strongly encouraged to always use the\n latest valid commit.\n * If an upper bound is specified, then the the tag\n corresponding to that upper bound is the latest commit that\n is valid",
"pattern": "^((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*(\\s*,\\s*((==|~=|<|>|<=|>=|~>)\\s*)?(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*))*)*$",
"type": "string"
},
"email": {
"description": "Author email",
"releases_only": {
"type": "boolean",
"description": "Whether the package manager should only resolve version\nconstraints to include tagged releases."
},
"source": {
"description": "Source of the dependency. See previous `source` description.",
"pattern": "^\\w+:\/\/",
"type": "string"
}
},
"type": "object",
"description": "Author of the package",
"additionalProperties": false
},
"license": {
"description": "This is [SPDX](https:\/\/spdx.org\/licenses\/) license identifier. Dual\nlicensing is indicated via joining the relevant licenses via `\/`.",
"type": "string"
},
"detailed": {
"description": "Long-form description of the package, this should convey the\npackage's principal functionality to the user without being as\ndetailed as the package readme.",
"type": "string"
"type": "object"
}
},
"type": "object",
"description": "Description of the package",
"additionalProperties": false
}
},
"source": {
"pattern": "^\\w+:\/\/",
"type": "string",
"description": "The URL of the package source archive. Examples:\n\"http:\/\/github.com\/downloads\/keplerproject\/wsapi\/wsapi-1.3.4.tar.gz\",\n\"git:\/\/github.com\/keplerproject\/wsapi.git\". Different protocols are\nsupported:\n\n * `luarocks:\/\/` - for luarocks packages\n * `file:\/\/` - for URLs in the local filesystem (note that for Unix\n paths, the root slash is the third slash, resulting in paths like\n \"file:\/\/\/full\/path\/filename\"\n * `git:\/\/` - for the Git source control manager\n * `git+https:\/\/` - for the Git source control manager when using\n repositories that need https:\/\/ URLs.\n * `git+ssh:\/\/` - for the Git source control manager when using\n repositories that need SSH login, such as [email protected]\/myrepo.\n * `http:\/\/` - for HTTP URLs\n * `https:\/\/` - for HTTPS URLs",
"examples": [
"luarocks:\/\/argparse",
"git:\/\/github.com\/nvim-lua\/plenary.nvim"
]
},
"$schema": {
"description": "The optional json schema URI for validation with json-language-server.",
],
"description": "The URL of the package source archive. Examples:\n\"http:\/\/github.com\/downloads\/keplerproject\/wsapi\/wsapi-1.3.4.tar.gz\",\n\"git:\/\/github.com\/keplerproject\/wsapi.git\". Different protocols are\nsupported:\n\n * `luarocks:\/\/` - for luarocks packages\n * `file:\/\/` - for URLs in the local filesystem (note that for Unix\n paths, the root slash is the third slash, resulting in paths like\n \"file:\/\/\/full\/path\/filename\"\n * `git:\/\/` - for the Git source control manager\n * `git+https:\/\/` - for the Git source control manager when using\n repositories that need https:\/\/ URLs.\n * `git+ssh:\/\/` - for the Git source control manager when using\n repositories that need SSH login, such as [email protected]\/myrepo.\n * `http:\/\/` - for HTTP URLs\n * `https:\/\/` - for HTTPS URLs",
"type": "string"
}
},
"additionalProperties": false,
"description": "A package specification for Neovim"
"title": "packspec",
"description": "A package specification for Neovim",
"required": [
"package",
"source"
],
"type": "object"
}
9 changes: 3 additions & 6 deletions scripts/generate_json_schema.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#! /usr/bin/env lua5.1

-- local cjson = require('cjson')
local schema = require('packspec.schema')
-- TODO(justinmk): could eliminate format_cjson if `vim.json.encode()` had formatting options.
local format_json = require('utils.format_cjson')
local encoded_json = format_json(schema, "\n", " ")

local file = io.open('schema/packspec_schema.json', 'w')

local encoded_json = format_json(schema, "\n", " ")..'\n'
local file = assert(io.open('schema/packspec_schema.json', 'w'))
io.output(file)
io.write(encoded_json)
io.close(file)
11 changes: 4 additions & 7 deletions scripts/packspec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#! /usr/bin/env lua5.1

local function fatal(s, ...)
print(string.format(s, ...))
os.exit(1)
Expand All @@ -10,15 +8,14 @@ if not arg[1] then
end

local jsonschema = require('jsonschema')
local cjson = require('cjson.safe')
local schema = require('packspec.schema')

local validator = jsonschema.generate_validator(schema)

for _, path in ipairs(arg) do
local spec
if path:match('%.lua$') then
local chunk, err = loadfile(path)
local chunk, err = assert(loadfile(path))
if err then
fatal('%s: loading failed\n%s', path, err)
end
Expand All @@ -32,18 +29,18 @@ for _, path in ipairs(arg) do
fatal('%s: evaluation failed\n%s', path, err)
end
elseif path:match('%.json$') then
local file, err = io.open(path, 'rb')
local file, err = assert(io.open(path, 'rb'))
if err then
fatal('%s: failed to open file\n%s', path, err)
end

local json = file:read('*a')
local json = (file:read('*a'))
file:close()
if not json then
fatal('%s: could not read the file', path)
end

spec, err = cjson.decode(json)
spec, err = vim.json.decode(json)
if err then
fatal('%s: decoding json failed\n%s', path, err)
end
Expand Down
1 change: 1 addition & 0 deletions test-npm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
10 changes: 10 additions & 0 deletions test-npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Test pkg.json against NPM's read-package-json library
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test-npm/ micro-project is just a way to get an idea of how close pkg.json is to package.json. Ideally it would be a strict subset but probably can't be. Any divergences will be reflected in the test files here.


`pkg.json` tries to be a subset of NPM's `package.json` format. The tests in
this directory test `pkg.json` samples using NPM's `read-package-json` library.

To run the tests:

cd test-npm/
npm ci
npm run test
Loading