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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Validate manifests against JSON Schema in CI Tests #1331

Merged
merged 12 commits into from
Feb 17, 2017
Merged

[WIP] Validate manifests against JSON Schema in CI Tests #1331

merged 12 commits into from
Feb 17, 2017

Conversation

r15ch13
Copy link
Member

@r15ch13 r15ch13 commented Feb 12, 2017

I am implementing manifest validation against the JSON Schema using dlls from Newtonsoft.Json.Schema.
Currently it works on my machine, but it fails on appveyor. (See my build log or the log of this PR)

Could this have anything to do with different powershell or .NET versions?
PS on appveyor:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      0      10586  117

PS on my machine:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      14393  693

I have no idea why this error Cannot find an overload for "IsValid" and the argument count: "3". pops up.

Any ideas? 馃槃

Copy link
Member

@deevus deevus left a comment

Choose a reason for hiding this comment

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

Try installing a specific version that works locally.

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 12, 2017 via email

@rrelmy
Copy link
Collaborator

rrelmy commented Feb 12, 2017

Sorry, I can't help here.
I have no experience with AppVeyor or. NET ;-)

@lukesampson
Copy link
Member

Is it possible that the AppVeyor build environment already has an older version of Newtonsoft.Json.Schema installed and it's taking precedence even after you Add-Type the specific version?

Is it worth adding something like this after you run the Add-Type to check the file path of the loaded assembly?

[appdomain]::currentdomain.getassemblies() |? { $_.getname().name -eq 'Newtonsoft.Json.Schema' } |% { $_.codebase }

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 13, 2017

Nice good idea!
https://ci.appveyor.com/project/r15ch13/scoop/build/15-test#L181
Newtonsoft.Json 8.0 is already there 馃憥

But Newtonsoft.Json.Schema is not installed ...
Edit:
Uuuh i found it in SchemaExtensions.cs#L41
It extends JToken (this JToken source) and i try to call it with 3 instead of 2 parameters because i overlooked the this keyword ... 馃槩

Other strange things:

  • It works on my system
  • It works in C#
IList<string> errors;
SchemaExtensions.IsValid(JToken.Parse(strJson), JSchema.Parse(strSchema), out errors);

So i guess PS 5.1 can use static extensions, but 5.0 can't ...

Also using IsValid as an extension doesn't work because $manifest is now of type Newtonsoft.Json.Linq.JObject which doesn't have the IsValid extension... Can i cast it in someway to JToken using Powershell?
RuntimeException: Method invocation failed because [Newtonsoft.Json.Linq.JObject] does not contain a method named 'IsValid'.

$manifest = [Newtonsoft.Json.Linq.JToken]::Parse($json)
$schema = [Newtonsoft.Json.Schema.JSchema]::Parse($schema_json)
$manifest::IsValid($schema, [ref]$validationErrors)

JToken manifest = JToken.Parse(strJson);
JSchema schema = JSchema.Parse(strSchema);
manifest.IsValid(schema, out errors);

Or should i write it as a small C# validation tool (like shim.exe)?

@deevus
Copy link
Member

deevus commented Feb 13, 2017

Try calling it using the static class?

[NewtonSoft.Json.Schema.SchemaExtensions]::IsValid($manifest)

$json = gc "$working_dir\invalid_wget.json" -raw -Encoding UTF8
$manifest = [Newtonsoft.Json.Linq.JToken]::Parse($json)
$schema = [Newtonsoft.Json.Schema.JSchema]::Parse($schema_json)
[Newtonsoft.Json.Schema.SchemaExtensions]::IsValid($manifest, $schema, [ref]$validationErrors)
Copy link
Member Author

Choose a reason for hiding this comment

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

Already doing that

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 13, 2017

Okay i have written a small c# tool for validating the manifests.
Calling a process for each manifest increased the duration from around 10-20ms to 250-260ms (on an SSD) per test ...

I will try it with a custom library now... 馃槃

* Added validator.exe as a single file validation tool

* Added Scoop.Validator Lib for use in Manifest-Tests and validator.exe

* Exclude .dll and packages folder from Project-Tests

Add buildscript for Scoop.Validator and validator.exe
@r15ch13
Copy link
Member Author

r15ch13 commented Feb 13, 2017

Curse you Appveyor!

Again: no idea ...

@lukesampson
Copy link
Member

Wow, that's frustrating. Could it be an x86/x64 difference between what AppVeyor is using and what the Scoop.Validator.dll is targeting? Would setting a platform in appveyor.yml make a difference, or making Scoop.Validator.dll match whatever platform the Newtonsoft dlls target?

@r15ch13 r15ch13 changed the title Validate manifests against JSON Schema in CI Tests [WIP] Validate manifests against JSON Schema in CI Tests Feb 15, 2017
@rasa
Copy link
Member

rasa commented Feb 15, 2017

If you can't get it working, perhaps consider using python for this test, as https://github.com/rasa/scoops/blob/master/validate.py is MIT licensed.

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 15, 2017

Ah nice 馃憤
Results from trying it out:

  • changed validate import to Draft4Validator (because we use the "required" field)
  • writing the exception stacktrace to .failed raises an exception 馃槃 (using Python 3.6)
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "validate.py", line 51, in <module>
    f.write(trace)
TypeError: a bytes-like object is required, not 'str'

@rasa
Copy link
Member

rasa commented Feb 15, 2017

Sorry, I've only tested it with Python 2.7.13. Remove the b in line 50:

with open(failed, 'a+') as f:

and it should work in Python 3

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 15, 2017

Yup that worked. I'm going to redo this in python tomorrow or so. I hope that works on appveyor. 馃榾

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 17, 2017

Apparently git has desided to destroy all dll files:

warning: LF will be replaced by CRLF in supporting/validator/Newtonsoft.Json.dll.
The file will have its original line endings in your working directory.

Edit: Aah the .gitattributes might be the problem. *.dll is not set.

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 17, 2017

Eureka! It works!

@r15ch13 r15ch13 merged commit 1feda7a into ScoopInstaller:master Feb 17, 2017
@r15ch13 r15ch13 deleted the schema-validation branch February 17, 2017 18:05
@rasa
Copy link
Member

rasa commented Feb 17, 2017

Two thoughts:

  1. Was removing UniqueItems intentional? If so, what's the use case for duplicates? Does the code support dups?
  2. Requiring version means we can't have pure meta-packages, with just a depends list.

@r15ch13
Copy link
Member Author

r15ch13 commented Feb 18, 2017

  1. I have no idea why i removed uniqueItems 馃槙 Will add it back.
  2. Without version the installation won't run at all (see lib/install.ps1#L20)

Added some changes to the schema in #1342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants