integration_test.go (2208B)
1 // Copyright 2021 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package openapi3_test 15 16 import ( 17 "strings" 18 "testing" 19 20 "github.com/gohugoio/hugo/hugolib" 21 ) 22 23 func TestUnmarshal(t *testing.T) { 24 t.Parallel() 25 26 files := ` 27 -- assets/api/myapi.yaml -- 28 openapi: 3.0.0 29 info: 30 title: Sample API 31 description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML. 32 version: 0.1.9 33 servers: 34 - url: http://api.example.com/v1 35 description: Optional server description, e.g. Main (production) server 36 - url: http://staging-api.example.com 37 description: Optional server description, e.g. Internal staging server for testing 38 paths: 39 /users: 40 get: 41 summary: Returns a list of users. 42 description: Optional extended description in CommonMark or HTML. 43 responses: 44 '200': # status code 45 description: A JSON array of user names 46 content: 47 application/json: 48 schema: 49 type: array 50 items: 51 type: string 52 -- config.toml -- 53 baseURL = 'http://example.com/' 54 -- layouts/index.html -- 55 {{ $api := resources.Get "api/myapi.yaml" | openapi3.Unmarshal }} 56 API: {{ $api.Info.Title | safeHTML }} 57 ` 58 59 b := hugolib.NewIntegrationTestBuilder( 60 hugolib.IntegrationTestConfig{ 61 T: t, 62 Running: true, 63 TxtarString: files, 64 }, 65 ).Build() 66 67 b.AssertFileContent("public/index.html", `API: Sample API`) 68 69 b. 70 EditFileReplace("assets/api/myapi.yaml", func(s string) string { return strings.ReplaceAll(s, "Sample API", "Hugo API") }). 71 Build() 72 73 b.AssertFileContent("public/index.html", `API: Hugo API`) 74 }