Skip to content

Commit

Permalink
Merge pull request #148 from shnydercom/main
Browse files Browse the repository at this point in the history
CLI loads local file
Eyas authored Mar 9, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents f2feed1 + 69b3171 commit f8f93d6
Showing 6 changed files with 150 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ import {ArgumentParser} from 'argparse';
export interface Options {
/** HTTPS URL to an .nt file defining a custom ontology. */
ontology: string;
file: string | undefined;
verbose: boolean;
deprecated: boolean;
context: string;
@@ -78,6 +79,11 @@ export function ParseFlags(args?: string[]): Options {
metavar: 'https://url.to/schema.nt',
dest: 'ontology',
});
parser.add_argument('--file', {
default: undefined,
help: 'file path to a .nt file, for using a local ontology file',
dest: 'file',
});

const deprecated = parser.add_mutually_exclusive_group({required: false});
deprecated.add_argument('--deprecated', {
15 changes: 12 additions & 3 deletions src/cli/internal/main.ts
Original file line number Diff line number Diff line change
@@ -14,9 +14,11 @@
* limitations under the License.
*/

import {Observable} from 'rxjs';
import {Triple} from '../..';
import {Log, SetOptions} from '../../logging';
import {WriteDeclarations} from '../../transform/transform';
import {load} from '../../triples/reader';
import {load, loadFile} from '../../triples/reader';
import {Context} from '../../ts/context';

import {ParseFlags} from '../args';
@@ -26,9 +28,16 @@ export async function main(args?: string[]) {
SetOptions(options);

const ontologyUrl = options.ontology;
Log(`Loading Ontology from URL: ${ontologyUrl}`);
const filePath = options.file;
let result: Observable<Triple>;

const result = load(ontologyUrl);
if (filePath) {
Log(`Loading Ontology from path: ${filePath}`);
result = loadFile(filePath);
} else {
Log(`Loading Ontology from URL: ${ontologyUrl}`);
result = load(ontologyUrl);
}
const context = Context.Parse(options.context);
await WriteDeclarations(result, options.deprecated, context, write);
}
40 changes: 40 additions & 0 deletions src/triples/reader.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@
* limitations under the License.
*/
import https from 'https';
import fs from 'fs';
import readline from 'readline';
import {Observable, Subscriber, TeardownLogic} from 'rxjs';

import {Log} from '../logging';
@@ -75,6 +77,44 @@ export function load(url: string): Observable<Triple> {
});
}

/**
* does the same as load(), but for a local file
*/
export function loadFile(path: string): Observable<Triple> {
return new Observable<Triple>(subscriber => {
handleFile(path, subscriber);
});
}

function handleFile(
path: string,
subscriber: Subscriber<Triple>
): TeardownLogic {
const rl = readline.createInterface({
input: fs.createReadStream(path),
crlfDelay: Infinity,
});

const data: string[] = [];

rl.on('line', (line: string) => {
data.push(line);
});

rl.on('close', () => {
try {
const triples = toTripleStrings(data);
for (const triple of process(triples)) {
subscriber.next(triple);
}
} catch (error) {
Log(`Caught Error on end: ${error}`);
subscriber.error(error);
}
subscriber.complete();
});
}

function handleUrl(url: string, subscriber: Subscriber<Triple>): TeardownLogic {
https
.get(url, response => {
49 changes: 49 additions & 0 deletions test/cli/args_logmessages_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import {Readable} from 'stream';
import fs from 'fs';
import {main} from '../../src/cli/internal/main';
import * as Logging from '../../src/logging';
import * as Transform from '../../src/transform/transform';

describe('main Args logs', () => {
let readStreamCreatorFn: jest.SpyInstance;
beforeEach(() => {
const mockFileLine = `<http://schema.org/Thing> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .\n`;
const mockedStream = Readable.from([mockFileLine]);
readStreamCreatorFn = jest
.spyOn(fs, 'createReadStream')
//@ts-ignore
.mockImplementation(path => mockedStream);
});
it(`the path it is loading from`, async () => {
const logs = [''];
// log messages get caught for checking assert:
jest
.spyOn(Logging, 'Log')
.mockImplementation((msg: string) => void logs.push(msg));
// but doesn't write the output .ts-file:
jest
.spyOn(Transform, 'WriteDeclarations')
.mockImplementation(async (...args) => {});
await main(['--file', `ontology-file.nt`, `--verbose`]);
expect(logs.join('')).toMatchInlineSnapshot(
`"Loading Ontology from path: ontology-file.nt"`
);
});
});
8 changes: 8 additions & 0 deletions test/cli/args_test.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ describe('ParseFlags', () => {
expect(options.context).toBe('https://schema.org');
expect(options.deprecated).toBe(true);
expect(options.verbose).toBe(false);
expect(options.file).toBeUndefined();

expect(options.ontology).toBe(
'https://schema.org/version/latest/schemaorg-current-https.nt'
@@ -37,6 +38,13 @@ describe('ParseFlags', () => {
expect(options.ontology).toBe('https://google.com/foo');
});

it('custom file', () => {
const options = ParseFlags(['--file', './ontology.nt'])!;
expect(options).not.toBeUndefined();

expect(options.file).toBe('./ontology.nt');
});

describe('deprecated fields', () => {
let mockExit: jest.MockInstance<
ReturnType<typeof ArgumentParser.prototype.exit>,
37 changes: 35 additions & 2 deletions test/triples/reader_test.ts
Original file line number Diff line number Diff line change
@@ -17,9 +17,10 @@
import {ClientRequest, IncomingMessage} from 'http';
import https from 'https';
import {toArray} from 'rxjs/operators';
import {PassThrough, Writable} from 'stream';
import {PassThrough, Readable, Writable} from 'stream';

import {load} from '../../src/triples/reader';
import fs from 'fs';
import {load, loadFile} from '../../src/triples/reader';
import {Triple} from '../../src/triples/triple';
import {SchemaString, UrlNode} from '../../src/triples/types';
import {flush} from '../helpers/async';
@@ -434,6 +435,38 @@ describe('load', () => {
]);
});
});
describe('local file', () => {
let readStreamCreatorFn: jest.SpyInstance;
beforeEach(() => {
const mockFileLine = `<https://schema.org/Person> <https://schema.org/knowsAbout> <https://schema.org/Event> .\n`;
const mockedStream = Readable.from([mockFileLine]);
readStreamCreatorFn = jest
.spyOn(fs, 'createReadStream')
//@ts-ignore
.mockImplementation(path => mockedStream);
});
it('fails loading a file (containing .nt syntax errors)', async () => {
const failingMockPath = './bad-ontology.nt';
const failingMockLine = `<https://schema.org/knowsAbout> <https://sc`;
const failingMockedStream = Readable.from([failingMockLine]);
readStreamCreatorFn.mockImplementation(path => failingMockedStream);

const fileTriples = loadFile(failingMockPath).toPromise();
await expect(fileTriples).rejects.toThrow('Unexpected');
});
it('loads a file from the correct path', async () => {
const mockFilePath = './ontology.nt';

const fileTriples = loadFile(mockFilePath).toPromise();

expect(readStreamCreatorFn).toBeCalledWith(mockFilePath);
await expect(fileTriples).resolves.toEqual({
Subject: UrlNode.Parse('https://schema.org/Person'),
Predicate: UrlNode.Parse('https://schema.org/knowsAbout'),
Object: UrlNode.Parse('https://schema.org/Event')!,
});
});
});
});
});

0 comments on commit f8f93d6

Please sign in to comment.