Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stub in test from the 2nd time onwards #61

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/exporter/files.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs/promises'
import { test } from 'uvu'
import * as td from 'testdouble'
import * as assert from 'uvu/assert'
Expand All @@ -14,6 +15,31 @@ type GHRes = {
documentation_url: string
}

function urlToFilename(u: string) {
return u.replace('://', '_').replace(/\./g, '_').replace(/\//g, '_')
}

async function vcr (reqUrl: string) {
const dir = 'testdata/vcr'
const p = `${dir}/${urlToFilename(reqUrl)}.txt`
try {
const str = await fs.readFile(p, 'utf-8')
return str
} catch (_) {
}

try {
const str = await files.getHTTP(reqUrl)
await files.createDirWhenNotfound(dir)
await fs.writeFile(p, str)
return str
} catch (e) {
console.error(e.message, e.name)
}

return ''
}

test('getHtmlMeta returns title and desc', async () => {
const tests = [
[
Expand Down Expand Up @@ -71,12 +97,12 @@ test('getHtmlMeta returns title and desc', async () => {
'Online communities include people from many different backgrounds. The Go contributors are committed to providing a friendly, safe and welcoming environment for all, regardless of gender identity and expression, sexual orientation, disabilities, neurodiversity, physical appearance, body size, ethnicity, nationality, race, age, religion, or similar personal characteristics.',
'/images/html-image-dd81b68c375850259cdb26e64618e339c4eec3c3.webp',
'/images/html-icon-77014b367198f9878ea91bdccd6bb3fce2a5ee2a-7f5e06b5d5dc8f2fac131fd56215ae5ca767a98c.webp',
]
],
]

for (const t of tests) {
const [url, title, desc, image, icon] = t
const re = await files.getHtmlMeta(url)
const re = await files.getHtmlMeta(url, vcr)
assert.equal(re.title, title)
assert.equal(re.desc, desc)
assert.equal(re.image, image)
Expand Down
6 changes: 3 additions & 3 deletions src/exporter/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async function httpsGetWithFollowRedirects (reqUrl: string, redirectCount?: numb
}
}

async function getHTTP (reqUrl: string, redirectCount?: number): Promise<string> {
export async function getHTTP (reqUrl: string, redirectCount?: number): Promise<string> {
if (!redirectCount) {
redirectCount = 0
}
Expand Down Expand Up @@ -401,9 +401,9 @@ export const findImage = (html: string): string | null => {
return null
}

export const getHtmlMeta = async (reqUrl: string): Promise<{ title: string, desc: string, image: string, icon: string }> => {
export const getHtmlMeta = async (reqUrl: string, httpFunc?: (reqUrl: string) => Promise<string>): Promise<{ title: string, desc: string, image: string, icon: string }> => {
try {
const resbody = await getHTTP(reqUrl)
const resbody = httpFunc ? await httpFunc(reqUrl) : await getHTTP(reqUrl)
const body = resbody.replace(/\n/g, ' ')

const title = findHtmlByRegexp(titleRegexps, body) || ''
Expand Down
2,436 changes: 2,436 additions & 0 deletions testdata/vcr/https_github_com.txt

Large diffs are not rendered by default.

Loading
Loading