Skip to content
Snippets Groups Projects
Commit 6024edbf authored by Fabien Catteau's avatar Fabien Catteau :two:
Browse files

Merge branch '14630-use-gemnasium-db' into 'master'

Connect to gemnasium-db repo

See merge request !25
parents 69fe60b4 35d1a6c4
No related branches found
No related tags found
1 merge request!25Connect to gemnasium-db repo
Pipeline #91315861 passed
Pipeline: php-composer

#91317084

    Pipeline: ruby-bundler

    #91317082

      Pipeline: js-npm

      #91317080

        +1
        Showing
        with 5201 additions and 0 deletions
        {
        "name": "semver-parser",
        "version": "1.0.0",
        "description": "This package parses semver's as a stream from stdin",
        "main": "",
        "repository": "https://gitlab.com/leipert/code-bits/blob/master/semver-parser",
        "author": "Lukas Eipert <leipert@gitlab.com>",
        "license": "MIT",
        "private": true,
        "dependencies": {
        "path": "^0.12.7",
        "read-file": "^0.2.0",
        "semver": "^6.3.0",
        "test": "^0.6.0",
        "testing": "^1.1.2"
        }
        }
        #!/usr/bin/env node
        let documents;
        const bail = (error) => {
        console.log(error.message)
        process.exit(1);
        }
        var path = require('path');
        try {
        const fs = require('fs');
        if(!process.argv[2]){
        base = path.basename(process.argv[1])
        throw new Error("Usage: ./" + base + " <JSON input file>")
        }
        documents = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
        if(!Array.isArray(documents)){
        throw new Error('File input is not a JSON array.')
        }
        } catch(e){
        console.error(e.message);
        process.exit(1);
        }
        const {processDocument} = require('./shared');
        console.log(JSON.stringify(documents.map(processDocument),null,2))
        const semver = require('semver');
        module.exports = {
        processDocument(item) {
        try {
        if (!semver.validRange(item.range, true)) {
        throw new Error('malformed range string');
        }
        if (!semver.valid(item.version, true)) {
        throw new Error('malformed version string');
        }
        const satisfies = semver.satisfies(item.version, item.range, {
        includePrerelease: true, loose: true
        });
        return {'range': item.range, 'version': item.version, satisfies};
        } catch (e) {
        return {'range': item.range, 'version': item.version, 'error': e.message};
        }
        }
        }
        \ No newline at end of file
        #!/usr/bin/env node
        const {processDocument} = require('./shared');
        const writeResult = (res, lastDocument) => {
        process.stdout.write(JSON.stringify(res));
        process.stdout.write(lastDocument ? '\n' : '\r\n');
        };
        let restOfDocument = '';
        process.stdin.resume();
        process.stdin.setEncoding('utf8');
        process.stdin.on('data', function(chunk) {
        let documents = (restOfDocument + chunk).split('\r\n');
        restOfDocument = documents.pop();
        documents.forEach(d => {
        writeResult(processDocument(d));
        });
        });
        process.stdin.on('end', function() {
        writeResult(processDocument(restOfDocument), true);
        });
        #!/usr/bin/env node
        // run with ./test_rangecheck.js
        function clean_string(str) {
        return str.replace(/[^a-zA-Z0-9]/g, "")
        }
        var testing = require('testing');
        var fs = require("fs");
        function testWellFormedJsonFile(callback) {
        var expect = String(fs.readFileSync('tests/simple_out.json'));
        var spawn = require('child_process').spawnSync;
        var command = spawn('./rangecheck.js', ['tests/simple_in.json']);
        var out = String(command.stdout);
        var status = command.status;
        expect = clean_string(expect);
        out = clean_string(out);
        testing.assertEquals(status, 0);
        testing.assertEquals(out, expect);
        testing.success(callback);
        }
        function testDictionaryJsonFile(callback) {
        var spawn = require('child_process').spawnSync;
        const command = spawn('./rangecheck.js', ['tests/dictionary.json']);
        var out = String(command.stderr);
        testing.assertEquals(out, "Unexpected token { in JSON at position 2\n");
        testing.assertEquals(command.status, 1);
        testing.success(callback);
        }
        function testEmptyJsonFile(callback) {
        var spawn = require('child_process').spawnSync;
        const command = spawn('./rangecheck.js', ['tests/empty.json']);
        testing.assertEquals(String(command.stderr), "File input is not a JSON array.\n");
        testing.assertEquals(command.status, 1);
        testing.success(callback);
        }
        function testNonExistentJsonFile(callback) {
        var spawn = require('child_process').spawnSync;
        const command = spawn('./rangecheck.js', ['tests/empty00.json']);
        testing.assertEquals(String(command.stderr), "ENOENT: no such file or directory, open 'tests/empty00.json'\n");
        testing.assertEquals(command.status, 1);
        testing.success(callback);
        }
        function testGemnasiumDbAdvisoryRanges(callback) {
        var expect = String(fs.readFileSync('tests/adb_ranges_out.json'));
        var spawn = require('child_process').spawnSync;
        var command = spawn('./rangecheck.js', ['tests/adb_ranges_in.json']);
        var out = String(command.stdout);
        var status = command.status
        expect = clean_string(expect);
        out = clean_string(out)
        testing.assertEquals(status, 0);
        testing.assertEquals(out, expect);
        testing.success(callback);
        }
        exports.test = function(callback)
        {
        var tests = [
        testWellFormedJsonFile,
        testDictionaryJsonFile,
        testEmptyJsonFile,
        testNonExistentJsonFile,
        testGemnasiumDbAdvisoryRanges
        ];
        testing.run(tests, callback);
        };
        // run tests if invoked directly
        if (__filename == process.argv[1])
        {
        exports.test(testing.show);
        }
        This diff is collapsed.
        This diff is collapsed.
        {
        {"range": "==2.0.1", "version": "2.0.0"}
        }
        {}
        [{ "range": "^2.0.0", "version": "2.0.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "^2.0.0", "version": "1.1.0" },
        { "range": "Y.Z.YZ", "version": "1.1.0" },
        {
        "range": "^2.0.0 || ^3.0.0",
        "version": "2.0.0"
        },
        {
        "range": "^2.0.0-alpha.1",
        "version": "2.0.0"
        },
        {
        "range": "^2.0.0-alpha.1",
        "version": "2.0.0"
        },
        {
        "range": "< 2.0.0",
        "version": "1.0.0-alpha.1"
        }]
        [
        {
        "range": "^2.0.0",
        "version": "2.0.0",
        "satisfies": true
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "^2.0.0",
        "version": "1.1.0",
        "satisfies": false
        },
        {
        "range": "Y.Z.YZ",
        "version": "1.1.0",
        "error": "malformed range string"
        },
        {
        "range": "^2.0.0 || ^3.0.0",
        "version": "2.0.0",
        "satisfies": true
        },
        {
        "range": "^2.0.0-alpha.1",
        "version": "2.0.0",
        "satisfies": true
        },
        {
        "range": "^2.0.0-alpha.1",
        "version": "2.0.0",
        "satisfies": true
        },
        {
        "range": "< 2.0.0",
        "version": "1.0.0-alpha.1",
        "satisfies": true
        }
        ]
        # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
        # yarn lockfile v1
        semver@^6.3.0:
        version "6.3.0"
        resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
        integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
        <?php
        declare(strict_types=1);
        # Run with ./vendor/bin/phpunit --bootstrap vendor/autoload.php RangeCheckTest
        use PHPUnit\Framework\TestCase;
        final class RangeCheckTest extends TestCase
        {
        public function testWellFormedJsonFile(): void
        {
        $output = "";
        $exitcode = 0;
        exec('./rangecheck.php tests/simple_in.json', $output, $exitcode);
        $expectation = file_get_contents('tests/simple_out.json');
        $this->assertEquals(implode("\n", $output), $expectation);
        $this->assertEquals($exitcode, 0);
        }
        public function testDictionaryJsonFile(): void
        {
        $output = "";
        $exitcode = 0;
        exec('./rangecheck.php tests/dictionary.json', $output, $exitcode);
        $this->assertEquals(implode("\n", $output), "Malformed JSON file");
        $this->assertEquals($exitcode, 1);
        }
        public function testEmptyJsonFile(): void
        {
        $output = "";
        $exitcode = 0;
        exec('./rangecheck.php tests/empty.json', $output, $exitcode);
        $this->assertEquals(implode("\n", $output), "Malformed JSON file");
        $this->assertEquals($exitcode, 1);
        }
        public function testNonExistentJsonFile(): void
        {
        $output = "";
        $exitcode = 0;
        exec('./rangecheck.php tests/empty00.json', $output, $exitcode);
        $this->assertEquals(implode("\n", $output), "tests/empty00.json does not exist");
        $this->assertEquals($exitcode, 1);
        }
        public function testGemnasiumDbAdvisoryRanges(): void
        {
        $output = "";
        $exitcode = 0;
        exec('./rangecheck.php tests/adb_ranges_in.json', $output, $exitcode);
        $expectation = file_get_contents('tests/adb_ranges_out.json');
        $this->assertEquals(implode("\n", $output), $expectation);
        $this->assertEquals($exitcode, 0);
        }
        }
        {
        "description": "Range checker for PHP",
        "require": {
        "php": ">=7.0",
        "justinrainbow/json-schema": "^5.2",
        "salsify/json-streaming-parser": "^8.1",
        "composer/semver": "^1.5"
        },
        "require-dev": {
        "phpunit/phpunit": "^8"
        },
        "autoload": {
        "psr-4": { "JsonSchema\\": "src/JsonSchema/" }
        }
        }
        This diff is collapsed.
        #!/usr/bin/env php
        <?php
        require __DIR__ . '/vendor/autoload.php';
        use JsonSchema\SchemaStorage;
        use JsonSchema\Validator;
        use JsonSchema\Constraints\Factory;
        use Composer\Semver\Constraint\MultiConstraint;
        use Composer\Semver\Constraint\Constraint;
        use Composer\Semver\VersionParser;
        class JsonListener extends \JsonStreamingParser\Listener\IdleListener
        {
        private $range = null;
        private $version = null;
        private $ctx = '';
        private $dict = [];
        private static $parser = null;
        private $add_comma = false;
        static function init() {
        self::$parser = new VersionParser();
        }
        public function endObject(): void
        {
        if($this->add_comma)
        echo(",\n");
        if($this->range != null && $this->version != null) {
        $this->dict['satisfies'] = ($this->range->matches($this->version) ? true : false);
        }
        echo(json_encode(array($this->dict)[0], JSON_FORCE_OBJECT));
        $this->range = null;
        $this->version = null;
        $this->add_comma = true;
        $this->dict = [];
        }
        public function key(string $key): void
        {
        $this->ctx = $key;
        }
        public function value($value): void
        {
        if($this->ctx == "range"){
        $this->dict['range'] = $value;
        if (strlen($value) == 0) {
        $this->dict['error'] = 'range not specified';
        } else {
        try {
        $this->range = self::$parser->parseConstraints($value);
        } catch (Exception $e){
        $this->dict['error'] = $e->getMessage();
        }
        }
        } elseif ($this->ctx == "version") {
        $this->dict['version'] = $value;
        if (strlen($value) == 0) {
        $this->dict['error'] = 'version not specified';
        } else {
        $this->version = self::$parser->parseConstraints("== $value");
        }
        }
        $this->ctx = "";
        }
        public function startDocument(): void
        {
        echo("[\n");
        }
        public function endDocument(): void
        {
        echo("\n]");
        }
        }
        $jsonSchema = <<<'JSON'
        {
        "definitions": {},
        "$schema": "http://json-schema.org/draft-07/schema#",
        "$id": "http://example.com/root.json",
        "type": "array",
        "title": "The Root Schema",
        "items": {
        "$id": "#/items",
        "type": "object",
        "title": "The Items Schema",
        "required": [
        "range",
        "version"
        ],
        "properties": {
        "range": {
        "$id": "#/items/properties/range",
        "type": "string",
        "title": "Version Range Definition",
        "pattern": "^(.*)$"
        },
        "error": {
        "$id": "#/items/properties/error",
        "type": "string",
        "title": "Error Message",
        "pattern": "^(.*)$"
        },
        "version": {
        "$id": "#/items/properties/version",
        "type": "string",
        "title": "The Version Schema",
        "pattern": "^(.*)$"
        },
        "satisfies": {
        "$id": "#/items/properties/satisfies",
        "type": "boolean",
        "title": "true if version lies within range, false otherwise",
        "pattern": "^(true|false)$"
        }
        }
        }
        }
        JSON;
        JsonListener::init();
        if($argc != 2) {
        echo("Usage: ./rangecheck.php <JSON input file>");
        exit(1);
        }
        $inputFilename = $argv[1];
        if(file_exists($inputFilename) == false) {
        echo("$inputFilename does not exist");
        exit(1);
        }
        $jsonSchemaObject = json_decode($jsonSchema);
        $schemaStorage = new SchemaStorage();
        $schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);
        $jsonValidator = new Validator(new Factory($schemaStorage));
        try {
        $data = json_decode(file_get_contents($argv[1]));
        $jsonValidator->validate($data, $jsonSchemaObject);
        if($jsonValidator->isValid() == false) {
        echo("Malformed JSON file");
        exit(1);
        }
        } catch (Exception $e) {
        echo($e->getMessage());
        exit(1);
        }
        $stream = fopen($inputFilename, 'r');
        $listener = new JsonListener();
        try {
        $parser = new \JsonStreamingParser\Parser($stream, $listener);
        $parser->parse();
        fclose($stream);
        } catch (Exception $e) {
        fclose($stream);
        throw $e;
        }
        ?>
        This diff is collapsed.
        This diff is collapsed.
        {
        {"range": "==2.0.1", "version": "2.0.0"}
        }
        {}
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Finish editing this message first!
        Please register or to comment