Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
镜像
Coder
code-server
提交
4f320ad3
C
code-server
项目概览
镜像
/
Coder
/
code-server
2022-09-21 03:15:05同步失败
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
code-server
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
4f320ad3
编写于
5月 06, 2021
作者:
A
Asher
提交者:
GitHub
5月 06, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3297 from code-asher/test-static
上级
2f2fd066
e8443e26
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
212 addition
and
76 deletion
+212
-76
test/e2e/terminal.test.ts
test/e2e/terminal.test.ts
+3
-3
test/unit/constants.test.ts
test/unit/constants.test.ts
+19
-47
test/unit/helpers.test.ts
test/unit/helpers.test.ts
+14
-0
test/unit/register.test.ts
test/unit/register.test.ts
+3
-1
test/unit/routes/health.test.ts
test/unit/routes/health.test.ts
+2
-2
test/unit/routes/static.test.ts
test/unit/routes/static.test.ts
+136
-0
test/unit/util.test.ts
test/unit/util.test.ts
+3
-1
test/utils/constants.ts
test/utils/constants.ts
+0
-11
test/utils/helpers.ts
test/utils/helpers.ts
+31
-10
test/utils/integration.ts
test/utils/integration.ts
+1
-1
未找到文件。
test/e2e/terminal.test.ts
浏览文件 @
4f320ad3
import
{
test
,
expec
t
}
from
"
@playwright/test
"
import
{
expect
,
tes
t
}
from
"
@playwright/test
"
import
*
as
cp
from
"
child_process
"
import
*
as
fs
from
"
fs
"
// import { tmpdir } from "os"
import
*
as
path
from
"
path
"
import
util
from
"
util
"
import
{
STORAGE
,
tmpdir
}
from
"
../utils/constants
"
import
{
STORAGE
}
from
"
../utils/constants
"
import
{
tmpdir
}
from
"
../utils/helpers
"
import
{
CodeServer
}
from
"
./models/CodeServer
"
test
.
describe
(
"
Integrated Terminal
"
,
()
=>
{
...
...
test/unit/constants.test.ts
浏览文件 @
4f320ad3
import
*
as
fs
from
"
fs
"
import
{
tmpdir
}
from
"
../../test/utils/constants
"
import
{
loggerModule
}
from
"
../utils/helpers
"
// jest.mock is hoisted above the imports so we must use `require` here.
jest
.
mock
(
"
@coder/logger
"
,
()
=>
require
(
"
../utils/helpers
"
).
loggerModule
)
import
{
createLoggerMock
}
from
"
../utils/helpers
"
describe
(
"
constants
"
,
()
=>
{
beforeAll
(()
=>
{
jest
.
clearAllMocks
()
jest
.
resetModules
()
})
let
constants
:
typeof
import
(
"
../../src/node/constants
"
)
describe
(
"
with package.json defined
"
,
()
=>
{
const
{
getPackageJson
}
=
require
(
"
../../src/node/constants
"
)
le
t
mockPackageJson
=
{
const
loggerModule
=
createLoggerMock
(
)
cons
t
mockPackageJson
=
{
name
:
"
mock-code-server
"
,
description
:
"
Run VS Code on a remote server.
"
,
repository
:
"
https://github.com/cdr/code-server
"
,
version
:
"
1.0.0
"
,
commit
:
"
f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b
"
,
}
let
version
=
""
let
commit
=
""
beforeEach
(()
=>
{
beforeAll
(()
=>
{
jest
.
mock
(
"
@coder/logger
"
,
()
=>
loggerModule
)
jest
.
mock
(
"
../../package.json
"
,
()
=>
mockPackageJson
,
{
virtual
:
true
})
commit
=
require
(
"
../../src/node/constants
"
).
commit
version
=
require
(
"
../../src/node/constants
"
).
version
constants
=
require
(
"
../../src/node/constants
"
)
})
afterAll
(()
=>
{
...
...
@@ -34,18 +25,18 @@ describe("constants", () => {
})
it
(
"
should provide the commit
"
,
()
=>
{
expect
(
co
mmit
).
toBe
(
"
f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b
"
)
expect
(
co
nstants
.
commit
).
toBe
(
mockPackageJson
.
commit
)
})
it
(
"
should return the package.json version
"
,
()
=>
{
expect
(
version
).
toBe
(
mockPackageJson
.
version
)
expect
(
constants
.
version
).
toBe
(
mockPackageJson
.
version
)
})
describe
(
"
getPackageJson
"
,
()
=>
{
it
(
"
should log a warning if package.json not found
"
,
()
=>
{
const
expectedErrorMessage
=
"
Cannot find module './package.json' from 'src/node/constants.ts'
"
getPackageJson
(
"
./package.json
"
)
constants
.
getPackageJson
(
"
./package.json
"
)
expect
(
loggerModule
.
logger
.
warn
).
toHaveBeenCalled
()
expect
(
loggerModule
.
logger
.
warn
).
toHaveBeenCalledWith
(
expectedErrorMessage
)
...
...
@@ -54,51 +45,32 @@ describe("constants", () => {
it
(
"
should find the package.json
"
,
()
=>
{
// the function calls require from src/node/constants
// so to get the root package.json we need to use ../../
const
packageJson
=
getPackageJson
(
"
../../package.json
"
)
expect
(
Object
.
keys
(
packageJson
).
length
).
toBeGreaterThan
(
0
)
expect
(
packageJson
.
name
).
toBe
(
"
mock-code-server
"
)
expect
(
packageJson
.
description
).
toBe
(
"
Run VS Code on a remote server.
"
)
expect
(
packageJson
.
repository
).
toBe
(
"
https://github.com/cdr/code-server
"
)
const
packageJson
=
constants
.
getPackageJson
(
"
../../package.json
"
)
expect
(
packageJson
).
toStrictEqual
(
mockPackageJson
)
})
})
})
describe
(
"
with incomplete package.json
"
,
()
=>
{
le
t
mockPackageJson
=
{
cons
t
mockPackageJson
=
{
name
:
"
mock-code-server
"
,
}
let
version
=
""
let
commit
=
""
before
Each
(()
=>
{
before
All
(()
=>
{
jest
.
mock
(
"
../../package.json
"
,
()
=>
mockPackageJson
,
{
virtual
:
true
})
version
=
require
(
"
../../src/node/constants
"
).
version
commit
=
require
(
"
../../src/node/constants
"
).
commit
constants
=
require
(
"
../../src/node/constants
"
)
})
after
Each
(()
=>
{
after
All
(()
=>
{
jest
.
clearAllMocks
()
jest
.
resetModules
()
})
it
(
"
version should return 'development'
"
,
()
=>
{
expect
(
version
).
toBe
(
"
development
"
)
expect
(
constants
.
version
).
toBe
(
"
development
"
)
})
it
(
"
commit should return 'development'
"
,
()
=>
{
expect
(
commit
).
toBe
(
"
development
"
)
})
})
})
describe
(
"
test constants
"
,
()
=>
{
describe
(
"
tmpdir
"
,
()
=>
{
it
(
"
should return a temp directory
"
,
async
()
=>
{
const
testName
=
"
temp-dir
"
const
pathToTempDir
=
await
tmpdir
(
testName
)
expect
(
pathToTempDir
).
toContain
(
testName
)
await
fs
.
promises
.
rmdir
(
pathToTempDir
)
expect
(
constants
.
commit
).
toBe
(
"
development
"
)
})
})
})
test/unit/helpers.test.ts
0 → 100644
浏览文件 @
4f320ad3
import
{
promises
as
fs
}
from
"
fs
"
import
{
tmpdir
}
from
"
../../test/utils/helpers
"
/**
* This file is for testing test helpers (not core code).
*/
describe
(
"
test helpers
"
,
()
=>
{
it
(
"
should return a temp directory
"
,
async
()
=>
{
const
testName
=
"
temp-dir
"
const
pathToTempDir
=
await
tmpdir
(
testName
)
expect
(
pathToTempDir
).
toContain
(
testName
)
expect
(
fs
.
access
(
pathToTempDir
)).
resolves
.
toStrictEqual
(
undefined
)
})
})
test/unit/register.test.ts
浏览文件 @
4f320ad3
import
{
JSDOM
}
from
"
jsdom
"
import
{
registerServiceWorker
}
from
"
../../src/browser/register
"
import
{
loggerModule
}
from
"
../utils/helpers
"
import
{
createLoggerMock
}
from
"
../utils/helpers
"
import
{
LocationLike
}
from
"
./util.test
"
describe
(
"
register
"
,
()
=>
{
...
...
@@ -21,6 +21,7 @@ describe("register", () => {
})
})
const
loggerModule
=
createLoggerMock
()
beforeEach
(()
=>
{
jest
.
clearAllMocks
()
jest
.
mock
(
"
@coder/logger
"
,
()
=>
loggerModule
)
...
...
@@ -75,6 +76,7 @@ describe("register", () => {
})
describe
(
"
when navigator and serviceWorker are NOT defined
"
,
()
=>
{
const
loggerModule
=
createLoggerMock
()
beforeEach
(()
=>
{
jest
.
clearAllMocks
()
jest
.
mock
(
"
@coder/logger
"
,
()
=>
loggerModule
)
...
...
test/unit/health.test.ts
→
test/unit/
routes/
health.test.ts
浏览文件 @
4f320ad3
import
*
as
httpserver
from
"
../utils/httpserver
"
import
*
as
integration
from
"
../utils/integration
"
import
*
as
httpserver
from
"
../
../
utils/httpserver
"
import
*
as
integration
from
"
../
../
utils/integration
"
describe
(
"
health
"
,
()
=>
{
let
codeServer
:
httpserver
.
HttpServer
|
undefined
...
...
test/unit/routes/static.test.ts
0 → 100644
浏览文件 @
4f320ad3
import
{
promises
as
fs
}
from
"
fs
"
import
*
as
path
from
"
path
"
import
{
tmpdir
}
from
"
../../utils/helpers
"
import
*
as
httpserver
from
"
../../utils/httpserver
"
import
*
as
integration
from
"
../../utils/integration
"
describe
(
"
/static
"
,
()
=>
{
let
_codeServer
:
httpserver
.
HttpServer
|
undefined
function
codeServer
():
httpserver
.
HttpServer
{
if
(
!
_codeServer
)
{
throw
new
Error
(
"
tried to use code-server before setting it up
"
)
}
return
_codeServer
}
let
testFile
:
string
|
undefined
let
testFileContent
:
string
|
undefined
let
nonExistentTestFile
:
string
|
undefined
// The static endpoint expects a commit and then the full path of the file.
// The commit is just for cache busting so we can use anything we want. `-`
// and `development` are specially recognized in that they will cause the
// static endpoint to avoid sending cache headers.
const
commit
=
"
-
"
beforeAll
(
async
()
=>
{
const
testDir
=
await
tmpdir
(
"
static
"
)
testFile
=
path
.
join
(
testDir
,
"
test
"
)
testFileContent
=
"
static file contents
"
nonExistentTestFile
=
path
.
join
(
testDir
,
"
i-am-not-here
"
)
await
fs
.
writeFile
(
testFile
,
testFileContent
)
})
afterEach
(
async
()
=>
{
if
(
_codeServer
)
{
await
_codeServer
.
close
()
_codeServer
=
undefined
}
})
function
commonTests
()
{
it
(
"
should return a 404 when a commit and file are not provided
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
"
/static
"
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Not Found
"
})
})
it
(
"
should return a 404 when a file is not provided
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
`
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Not Found
"
})
})
}
describe
(
"
disabled authentication
"
,
()
=>
{
beforeEach
(
async
()
=>
{
_codeServer
=
await
integration
.
setup
([
"
--auth=none
"
],
""
)
})
commonTests
()
it
(
"
should return a 404 for a nonexistent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
/
${
nonExistentTestFile
}
`
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
.
error
).
toMatch
(
"
ENOENT
"
)
})
it
(
"
should return a 200 and file contents for an existent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}${
testFile
}
`
)
expect
(
resp
.
status
).
toBe
(
200
)
const
content
=
await
resp
.
text
()
expect
(
content
).
toStrictEqual
(
testFileContent
)
})
})
describe
(
"
enabled authentication
"
,
()
=>
{
// Store whatever might be in here so we can restore it afterward.
// TODO: We should probably pass this as an argument somehow instead of
// manipulating the environment.
const
previousEnvPassword
=
process
.
env
.
PASSWORD
beforeEach
(
async
()
=>
{
process
.
env
.
PASSWORD
=
"
test
"
_codeServer
=
await
integration
.
setup
([
"
--auth=password
"
],
""
)
})
afterEach
(()
=>
{
process
.
env
.
PASSWORD
=
previousEnvPassword
})
commonTests
()
describe
(
"
inside code-server root
"
,
()
=>
{
it
(
"
should return a 404 for a nonexistent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
/
${
__filename
}
-does-not-exist`
)
expect
(
resp
.
status
).
toBe
(
404
)
const
content
=
await
resp
.
json
()
expect
(
content
.
error
).
toMatch
(
"
ENOENT
"
)
})
it
(
"
should return a 200 and file contents for an existent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}${
__filename
}
`
)
expect
(
resp
.
status
).
toBe
(
200
)
const
content
=
await
resp
.
text
()
expect
(
content
).
toStrictEqual
(
await
fs
.
readFile
(
__filename
,
"
utf8
"
))
})
})
describe
(
"
outside code-server root
"
,
()
=>
{
it
(
"
should return a 401 for a nonexistent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}
/
${
nonExistentTestFile
}
`
)
expect
(
resp
.
status
).
toBe
(
401
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Unauthorized
"
})
})
it
(
"
should return a 401 for an existent file
"
,
async
()
=>
{
const
resp
=
await
codeServer
().
fetch
(
`/static/
${
commit
}${
testFile
}
`
)
expect
(
resp
.
status
).
toBe
(
401
)
const
content
=
await
resp
.
json
()
expect
(
content
).
toStrictEqual
({
error
:
"
Unauthorized
"
})
})
})
})
})
test/unit/util.test.ts
浏览文件 @
4f320ad3
...
...
@@ -11,7 +11,7 @@ import {
trimSlashes
,
normalize
,
}
from
"
../../src/common/util
"
import
{
loggerModule
}
from
"
../utils/helpers
"
import
{
createLoggerMock
}
from
"
../utils/helpers
"
const
dom
=
new
JSDOM
()
global
.
document
=
dom
.
window
.
document
...
...
@@ -229,6 +229,8 @@ describe("util", () => {
jest
.
restoreAllMocks
()
})
const
loggerModule
=
createLoggerMock
()
it
(
"
should log an error with the message and stack trace
"
,
()
=>
{
const
message
=
"
You don't have access to that folder.
"
const
error
=
new
Error
(
message
)
...
...
test/utils/constants.ts
浏览文件 @
4f320ad3
import
*
as
fs
from
"
fs
"
import
*
as
os
from
"
os
"
import
*
as
path
from
"
path
"
export
const
CODE_SERVER_ADDRESS
=
process
.
env
.
CODE_SERVER_ADDRESS
||
"
http://localhost:8080
"
export
const
PASSWORD
=
process
.
env
.
PASSWORD
||
"
e45432jklfdsab
"
export
const
STORAGE
=
process
.
env
.
STORAGE
||
""
export
async
function
tmpdir
(
testName
:
string
):
Promise
<
string
>
{
const
dir
=
path
.
join
(
os
.
tmpdir
(),
"
code-server
"
)
await
fs
.
promises
.
mkdir
(
dir
,
{
recursive
:
true
})
return
await
fs
.
promises
.
mkdtemp
(
path
.
join
(
dir
,
`test-
${
testName
}
-`
),
{
encoding
:
"
utf8
"
})
}
test/utils/helpers.ts
浏览文件 @
4f320ad3
export
const
loggerModule
=
{
field
:
jest
.
fn
(),
level
:
2
,
logger
:
{
debug
:
jest
.
fn
(),
error
:
jest
.
fn
(),
info
:
jest
.
fn
(),
trace
:
jest
.
fn
(),
warn
:
jest
.
fn
(),
},
import
*
as
fs
from
"
fs
"
import
*
as
os
from
"
os
"
import
*
as
path
from
"
path
"
/**
* Return a mock of @coder/logger.
*/
export
function
createLoggerMock
()
{
return
{
field
:
jest
.
fn
(),
level
:
2
,
logger
:
{
debug
:
jest
.
fn
(),
error
:
jest
.
fn
(),
info
:
jest
.
fn
(),
trace
:
jest
.
fn
(),
warn
:
jest
.
fn
(),
},
}
}
/**
* Create a uniquely named temporary directory.
*
* These directories are placed under a single temporary code-server directory.
*/
export
async
function
tmpdir
(
testName
:
string
):
Promise
<
string
>
{
const
dir
=
path
.
join
(
os
.
tmpdir
(),
"
code-server/tests
"
)
await
fs
.
promises
.
mkdir
(
dir
,
{
recursive
:
true
})
return
await
fs
.
promises
.
mkdtemp
(
path
.
join
(
dir
,
`
${
testName
}
-`
),
{
encoding
:
"
utf8
"
})
}
test/utils/integration.ts
浏览文件 @
4f320ad3
...
...
@@ -3,7 +3,7 @@ import { runCodeServer } from "../../src/node/main"
import
*
as
httpserver
from
"
./httpserver
"
export
async
function
setup
(
argv
:
string
[],
configFile
?:
string
):
Promise
<
httpserver
.
HttpServer
>
{
argv
=
[
"
--bind-addr=localhost:0
"
,
...
argv
]
argv
=
[
"
--bind-addr=localhost:0
"
,
"
--log=warn
"
,
...
argv
]
const
cliArgs
=
parse
(
argv
)
const
configArgs
=
parseConfigFile
(
configFile
||
""
,
"
test/integration.ts
"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录