From 497c78d7603374f45c05be059736dd66a8a6b261 Mon Sep 17 00:00:00 2001 From: chaodaiG <45011425+chaodaiG@users.noreply.github.com> Date: Tue, 24 Sep 2019 14:28:42 -0700 Subject: [PATCH] Update Helloworld unit test error message to make it actionable (#1812) * Update Helloworld unit test error message to make it actionable Update error message with detailed explanation so that developers know how to debug if unit test ever failed. Fix a bug in unit test. Update all READMEs so that they are valid * Feedback updates --- .../hello-world/helloworld-nodejs/README.md | 10 ++++----- .../hello-world/helloworld-python/Dockerfile | 2 +- test/sampleapp/config.go | 10 +++++++++ test/sampleapp/config.yaml | 11 ++++++++-- .../sampleconsistency_test.go | 22 +++++++++++++------ 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/docs/serving/samples/hello-world/helloworld-nodejs/README.md b/docs/serving/samples/hello-world/helloworld-nodejs/README.md index 7bbfd0555..947856e1e 100644 --- a/docs/serving/samples/hello-world/helloworld-nodejs/README.md +++ b/docs/serving/samples/hello-world/helloworld-nodejs/README.md @@ -54,19 +54,19 @@ cd knative-docs/docs/serving/samples/hello-world/helloworld-nodejs 1. Create a new file named `index.js` and paste the following code: ```js - const express = require("express"); + const express = require('express'); const app = express(); - app.get("/", (req, res) => { - console.log("Hello world received a request."); + app.get('/', (req, res) => { + console.log('Hello world received a request.'); - const target = process.env.TARGET || "World"; + const target = process.env.TARGET || 'World'; res.send(`Hello ${target}!`); }); const port = process.env.PORT || 8080; app.listen(port, () => { - console.log("Hello world listening on port", port); + console.log('Hello world listening on port', port); }); ``` diff --git a/docs/serving/samples/hello-world/helloworld-python/Dockerfile b/docs/serving/samples/hello-world/helloworld-python/Dockerfile index ee8b2ebbf..5b4c0fb15 100644 --- a/docs/serving/samples/hello-world/helloworld-python/Dockerfile +++ b/docs/serving/samples/hello-world/helloworld-python/Dockerfile @@ -1,4 +1,4 @@ -# Use the official Python image. +# Use the official lightweight Python image. # https://hub.docker.com/_/python FROM python:3.7-slim diff --git a/test/sampleapp/config.go b/test/sampleapp/config.go index a5a6f675e..52b2d44a2 100644 --- a/test/sampleapp/config.go +++ b/test/sampleapp/config.go @@ -32,6 +32,16 @@ const ( defaultWorkDir = "helloworld-%s_tmp" defaultAppName = "helloworld-%s" defaultYamlImagePlaceHolder = "docker.io/{username}/helloworld-%s" + + // ActionMsg serves as documentation purpose, which will be referenced for + // clearly displaying error messages. + ActionMsg = "All files required for running sample apps are checked " + + "against README.md, the content of source files should be identical with what's " + + "in README.md file, the list of the files to be verified is the same set of files " + + "used for running sample apps, they are configured in `/test/sampleapp/config.yaml`. " + + "If an exception is needed the file can be configured to be copied as a separate step " + + "in `PreCommand` such as: " + + "https://github.com/knative/docs/blob/65f7b402fee7f94dfbd9e4512ef3beed7b85de66/test/sampleapp/config.yaml#L4" ) // AllConfigs contains all LanguageConfig diff --git a/test/sampleapp/config.yaml b/test/sampleapp/config.yaml index 5c7ed314c..1bd311cdf 100644 --- a/test/sampleapp/config.yaml +++ b/test/sampleapp/config.yaml @@ -10,8 +10,10 @@ languages: - "Dockerfile" - language: "go" expectedOutput: "Hello Go Sample v1!" + preCommands: + - exec: "cp" + args: "../../docs/serving/samples/hello-world/helloworld-go/go.mod helloworld-go_tmp/go.mod" copies: - - "go.mod" - "helloworld.go" - "service.yaml" - "Dockerfile" @@ -24,6 +26,9 @@ languages: - "Dockerfile" - language: "nodejs" expectedOutput: "Hello Node.js Sample v1!" + preCommands: + - exec: "cp" + args: "../../docs/serving/samples/hello-world/helloworld-nodejs/package-lock.json helloworld-nodejs_tmp/package-lock.json" copies: - "index.js" - "package.json" @@ -43,10 +48,12 @@ languages: - "Dockerfile" - language: "ruby" expectedOutput: "Hello Ruby Sample v1!" + preCommands: + - exec: "cp" + args: "../../docs/serving/samples/hello-world/helloworld-ruby/Gemfile.lock helloworld-ruby_tmp/Gemfile.lock" copies: - "app.rb" - "Gemfile" - - "Gemfile.lock" - "service.yaml" - "Dockerfile" - language: "shell" diff --git a/test/sampleconsistency/sampleconsistency_test.go b/test/sampleconsistency/sampleconsistency_test.go index 0e55a78b8..aaa38cf9c 100644 --- a/test/sampleconsistency/sampleconsistency_test.go +++ b/test/sampleconsistency/sampleconsistency_test.go @@ -57,16 +57,19 @@ func checkContains(t *testing.T, rl []string, src string) { ir := 0 is := 0 best := -1 - // Scans rl(README lines) for entire block matching sl(source lines) - // Pointer ir keeps on moving, and pointer moves only when there is a line match, otherwise back to 0. - // A match is found if pointer is moved to end. + // Scans rl(README lines) for entire block matching sl(source lines). + // Pointer ir: keeps on moving no matter what. + // Pointer is: moves only when there is a line match, otherwise back to 0. A + // match is found if pointer is moved to end. + // best: tracks where the best match is on source lines, it's always one + // more line ahead of real match for ir < len(rl) && is < len(sl) { nr := normalize(rl[ir]) ns := normalize(sl[is]) if "" == ns { is++ - if is > best { + if is > best { // Consider it a match if it's empty line best = is } continue @@ -75,7 +78,7 @@ func checkContains(t *testing.T, rl []string, src string) { ir++ continue } - if nr != ns { + if nr != ns { // Start over if a non-match is found is = 0 } else { is++ @@ -85,8 +88,13 @@ func checkContains(t *testing.T, rl []string, src string) { } ir++ } - if is < len(sl) && best < len(sl) && best != -1 { - t.Fatalf("README.md file is missing line %d ('%s') from file '%s'", best, sl[best], src) + + if best == -1 { + // missing line is line 0 + best = 0 + } + if best != len(sl) { + t.Fatalf("README.md file is missing line %d ('%s') from file '%s'\nAdditional info:\n%s", best, sl[best], src, sampleapp.ActionMsg) } }