From a8a28283e7ddf4cc5761c8063647bbd697892eb5 Mon Sep 17 00:00:00 2001 From: Alexander Tsybulsky Date: Sun, 19 Apr 2020 12:02:44 +0300 Subject: [PATCH] Minor tweaks (#26) * move front page from root * check object UT --- src/api/check_object.test.ts | 66 +++++++++++++++++++++++++++++++++++ src/{ => api}/front_page.ts | 4 +-- src/app.ts | 4 +-- test/check-object.api.test.ts | 41 ++++++++++++++++++++++ 4 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 src/api/check_object.test.ts rename src/{ => api}/front_page.ts (89%) create mode 100644 test/check-object.api.test.ts diff --git a/src/api/check_object.test.ts b/src/api/check_object.test.ts new file mode 100644 index 00000000..99894f47 --- /dev/null +++ b/src/api/check_object.test.ts @@ -0,0 +1,66 @@ +import { checkObject } from "./check_object"; + +test("should happily check object", () => { + const config = { + "syntax": { "version": "v702" }, + "rules": { + "indentation": true, + "keyword_case": { "style": "upper" } + } + }; + const file = ` +REPORT zxxx. + DATA lvstr TYPE string. +write 'Hello world'.`; + + const checkObjectInput = { + configuration: Buffer.from(JSON.stringify(config)).toString("base64"), + object: { + objectName: "ZXXX", + objectType: "PROG", + }, + files: [ + { + name: "zxxx.prog.abap", + contents: Buffer.from(file).toString("base64"), + } + ] + }; + + const output = checkObject(checkObjectInput); + expect(output).toEqual({ + object: { + objectName: "ZXXX", + objectType: "PROG" + }, + issues: [ + { + message: "Keyword should be upper case: \"write\"", + key: "keyword_case", + start: { + row: 4, + col: 1 + }, + end: { + row: 4, + col: 6 + }, + "filename": "zxxx.prog.abap" + }, + { + message: "Indentation problem, expected 0 spaces", + key: "indentation", + start: { + row: 3, + col: 3 + }, + end: { + row: 3, + col: 26 + }, + filename: "zxxx.prog.abap" + } + ] + }); +}); + diff --git a/src/front_page.ts b/src/api/front_page.ts similarity index 89% rename from src/front_page.ts rename to src/api/front_page.ts index 12769834..87e64369 100644 --- a/src/front_page.ts +++ b/src/api/front_page.ts @@ -1,6 +1,6 @@ import * as abaplint from "@abaplint/core"; import * as os from "os"; -import { getLogTail } from "./lib/log-tail"; +import { getLogTail } from "../lib/log-tail"; function osInfo(): string { return "load: " + os.loadavg() + "
" + @@ -20,7 +20,7 @@ function renderLogTail(): string { } } -export function frontPage(): string { +export function renderFrontPage(): string { return ` diff --git a/src/app.ts b/src/app.ts index 376c5770..db917901 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import * as express from "express"; import * as morgan from "morgan"; import * as helmet from "helmet"; -import { frontPage } from "./front_page"; +import { renderFrontPage } from "./api/front_page"; import api from "./api"; import { addInfoEx } from "./lib/log-tail"; @@ -13,7 +13,7 @@ if (process.env.NODE_ENV !== "test") { app.use(morgan("common")); } -app.get("/", (_req, res) => res.send(frontPage())); +app.get("/", (_req, res) => res.send(renderFrontPage())); app.get("/healthz", (_req, res) => res.send("OK")); app.use("/api/v1", api); app.use("*", (req, res) => { diff --git a/test/check-object.api.test.ts b/test/check-object.api.test.ts new file mode 100644 index 00000000..6808d7e2 --- /dev/null +++ b/test/check-object.api.test.ts @@ -0,0 +1,41 @@ +import app from "../src/app"; +import * as request from "supertest"; + +test("should check file", async () => { + const config = { + "syntax": { "version": "v702" }, + "rules": { + "indentation": true, + "keyword_case": { "style": "upper" } + } + }; + const file = ` +REPORT zxxx. + DATA lvstr TYPE string. +write 'Hello world'.`; + + const checkObjectInput = { + configuration: Buffer.from(JSON.stringify(config)).toString("base64"), + object: { + objectName: "ZXXX", + objectType: "PROG", + }, + files: [ + { + name: "zxxx.prog.abap", + contents: Buffer.from(file).toString("base64"), + } + ] + }; + + // maybe use https://github.com/Dean177/jest-to-match-shape-of + const res = await request(app).post("/api/v1/check_file").send(checkObjectInput); + expect(res.status).toBe(200); + expect(typeof res.body).toBe("object"); + expect(res.body.object).toEqual({ + objectName: "ZXXX", + objectType: "PROG", + }); + expect(Array.isArray(res.body.issues)).toBeTruthy(); + expect(res.body.issues.length).toBe(2); +});