1
0
mirror of https://git.tuxpa.in/a/code-server.git synced 2024-12-26 12:15:26 +00:00

feat(testing): refactor humanPath and add tests (#4511)

* feat: add test for humanPath

* refactor: make humanPath pure and pass in homedir
This commit is contained in:
Joe Previte 2021-11-15 19:40:34 +00:00 committed by GitHub
parent 16a5f2e171
commit 0a072f7532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 12 deletions

View File

@ -606,7 +606,7 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), { await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
flag: "wx", // wx means to fail if the path exists. flag: "wx", // wx means to fail if the path exists.
}) })
logger.info(`Wrote default config file to ${humanPath(configPath)}`) logger.info(`Wrote default config file to ${humanPath(os.homedir(), configPath)}`)
} catch (error: any) { } catch (error: any) {
// EEXIST is fine; we don't want to overwrite existing configurations. // EEXIST is fine; we don't want to overwrite existing configurations.
if (error.code !== "EEXIST") { if (error.code !== "EEXIST") {

View File

@ -1,4 +1,5 @@
import { field, logger } from "@coder/logger" import { field, logger } from "@coder/logger"
import * as os from "os"
import http from "http" import http from "http"
import path from "path" import path from "path"
import { Disposable } from "../common/emitter" import { Disposable } from "../common/emitter"
@ -95,8 +96,8 @@ export const runCodeServer = async (
): Promise<{ dispose: Disposable["dispose"]; server: http.Server }> => { ): Promise<{ dispose: Disposable["dispose"]; server: http.Server }> => {
logger.info(`code-server ${version} ${commit}`) logger.info(`code-server ${version} ${commit}`)
logger.info(`Using user-data-dir ${humanPath(args["user-data-dir"])}`) logger.info(`Using user-data-dir ${humanPath(os.homedir(), args["user-data-dir"])}`)
logger.trace(`Using extensions-dir ${humanPath(args["extensions-dir"])}`) logger.trace(`Using extensions-dir ${humanPath(os.homedir(), args["extensions-dir"])}`)
if (args.auth === AuthType.Password && !args.password && !args["hashed-password"]) { if (args.auth === AuthType.Password && !args.password && !args["hashed-password"]) {
throw new Error( throw new Error(
@ -109,7 +110,7 @@ export const runCodeServer = async (
const serverAddress = ensureAddress(app.server, protocol) const serverAddress = ensureAddress(app.server, protocol)
const disposeRoutes = await register(app, args) const disposeRoutes = await register(app, args)
logger.info(`Using config file ${humanPath(args.config)}`) logger.info(`Using config file ${humanPath(os.homedir(), args.config)}`)
logger.info( logger.info(
`${protocol.toUpperCase()} server listening on ${serverAddress.toString()} ${ `${protocol.toUpperCase()} server listening on ${serverAddress.toString()} ${
args.link ? "(randomized by --link)" : "" args.link ? "(randomized by --link)" : ""
@ -123,14 +124,14 @@ export const runCodeServer = async (
} else if (args.usingEnvHashedPassword) { } else if (args.usingEnvHashedPassword) {
logger.info(" - Using password from $HASHED_PASSWORD") logger.info(" - Using password from $HASHED_PASSWORD")
} else { } else {
logger.info(` - Using password from ${humanPath(args.config)}`) logger.info(` - Using password from ${humanPath(os.homedir(), args.config)}`)
} }
} else { } else {
logger.info(` - Authentication is disabled ${args.link ? "(disabled by --link)" : ""}`) logger.info(` - Authentication is disabled ${args.link ? "(disabled by --link)" : ""}`)
} }
if (args.cert) { if (args.cert) {
logger.info(` - Using certificate for HTTPS: ${humanPath(args.cert.value)}`) logger.info(` - Using certificate for HTTPS: ${humanPath(os.homedir(), args.cert.value)}`)
} else { } else {
logger.info(` - Not serving HTTPS ${args.link ? "(disabled by --link)" : ""}`) logger.info(` - Not serving HTTPS ${args.link ? "(disabled by --link)" : ""}`)
} }

View File

@ -1,6 +1,7 @@
import { Router, Request } from "express" import { Router, Request } from "express"
import { promises as fs } from "fs" import { promises as fs } from "fs"
import { RateLimiter as Limiter } from "limiter" import { RateLimiter as Limiter } from "limiter"
import * as os from "os"
import * as path from "path" import * as path from "path"
import { rootPath } from "../constants" import { rootPath } from "../constants"
import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http" import { authenticated, getCookieDomain, redirect, replaceTemplates } from "../http"
@ -30,7 +31,7 @@ export class RateLimiter {
const getRoot = async (req: Request, error?: Error): Promise<string> => { const getRoot = async (req: Request, error?: Error): Promise<string> => {
const content = await fs.readFile(path.join(rootPath, "src/browser/pages/login.html"), "utf8") const content = await fs.readFile(path.join(rootPath, "src/browser/pages/login.html"), "utf8")
let passwordMsg = `Check the config file at ${humanPath(req.args.config)} for the password.` let passwordMsg = `Check the config file at ${humanPath(os.homedir(), req.args.config)} for the password.`
if (req.args.usingEnvPassword) { if (req.args.usingEnvPassword) {
passwordMsg = "Password was set from $PASSWORD." passwordMsg = "Password was set from $PASSWORD."
} else if (req.args.usingEnvHashedPassword) { } else if (req.args.usingEnvHashedPassword) {

View File

@ -88,16 +88,17 @@ export function getEnvPaths(): Paths {
} }
/** /**
* humanPath replaces the home directory in p with ~. * humanPath replaces the home directory in path with ~.
* Makes it more readable. * Makes it more readable.
* *
* @param p * @param homedir - the home directory(i.e. `os.homedir()`)
* @param path - a file path
*/ */
export function humanPath(p?: string): string { export function humanPath(homedir: string, path?: string): string {
if (!p) { if (!path) {
return "" return ""
} }
return p.replace(os.homedir(), "~") return path.replace(homedir, "~")
} }
export const generateCertificate = async (hostname: string): Promise<{ cert: string; certKey: string }> => { export const generateCertificate = async (hostname: string): Promise<{ cert: string; certKey: string }> => {

View File

@ -476,3 +476,19 @@ describe("isFile", () => {
expect(await util.isFile(pathToFile)).toBe(true) expect(await util.isFile(pathToFile)).toBe(true)
}) })
}) })
describe("humanPath", () => {
it("should return an empty string if no path provided", () => {
const mockHomedir = "/home/coder"
const actual = util.humanPath(mockHomedir)
const expected = ""
expect(actual).toBe(expected)
})
it("should replace the homedir with ~", () => {
const mockHomedir = "/home/coder"
const path = `${mockHomedir}/code-server`
const actual = util.humanPath(mockHomedir, path)
const expected = "~/code-server"
expect(actual).toBe(expected)
})
})