Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
LinuxSuRen
jenkins
提交
a489cc4e
J
jenkins
项目概览
LinuxSuRen
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a489cc4e
编写于
1月 11, 2019
作者:
M
Matt Sicker
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move Windows util code into test sources
Signed-off-by:
N
Matt Sicker
<
boards@gmail.com
>
上级
b747845f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
53 addition
and
44 deletion
+53
-44
core/src/main/java/hudson/Util.java
core/src/main/java/hudson/Util.java
+0
-23
core/src/test/java/hudson/FilePathTest.java
core/src/test/java/hudson/FilePathTest.java
+2
-1
core/src/test/java/hudson/RemoveWindowsDirectoryJunctionTest.java
.../test/java/hudson/RemoveWindowsDirectoryJunctionTest.java
+3
-1
core/src/test/java/hudson/UtilTest.java
core/src/test/java/hudson/UtilTest.java
+3
-2
core/src/test/java/hudson/os/WindowsUtil.java
core/src/test/java/hudson/os/WindowsUtil.java
+38
-12
core/src/test/java/hudson/os/WindowsUtilTest.java
core/src/test/java/hudson/os/WindowsUtilTest.java
+7
-5
未找到文件。
core/src/main/java/hudson/Util.java
浏览文件 @
a489cc4e
...
...
@@ -30,7 +30,6 @@ import hudson.util.VariableResolver;
import
jenkins.util.SystemProperties
;
import
jenkins.util.io.PathRemover
;
import
jenkins.util.os.windows.WindowsCommandLineFormatter
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.output.NullOutputStream
;
...
...
@@ -1277,28 +1276,6 @@ public class Util {
}
}
/**
* Creates an NTFS junction point if supported. Similar to symbolic links, NTFS provides junction points which
* provide different features than symbolic links. This method is primarily useful for unit testing on Windows.
* @param junction NTFS junction point to create
* @param target target directory to junction
* @return the newly created junction point
* @throws IOException if the call to mklink exits with a non-zero status code
* @throws InterruptedException if the call to mklink is interrupted before completing
* @throws AssertionError if this method is called on a non-Windows platform
*/
static
@Nonnull
File
createJunction
(
@Nonnull
File
junction
,
@Nonnull
File
target
)
throws
IOException
,
InterruptedException
{
if
(!
Functions
.
isWindows
())
throw
new
AssertionError
(
"Cannot create junctions on non-Windows platforms"
);
String
command
=
"cmd.exe /C mklink /J "
+
WindowsCommandLineFormatter
.
quoteArgumentForCmd
(
junction
.
getAbsolutePath
())
+
' '
+
WindowsCommandLineFormatter
.
quoteArgumentForCmd
(
target
.
getAbsolutePath
());
Process
process
=
Runtime
.
getRuntime
().
exec
(
command
);
int
result
=
process
.
waitFor
();
if
(
result
!=
0
)
throw
new
IOException
(
"Command `"
+
command
+
"` failed with status code "
+
result
);
return
junction
;
}
/**
* Encodes the URL by RFC 2396.
*
...
...
core/src/test/java/hudson/FilePathTest.java
浏览文件 @
a489cc4e
...
...
@@ -26,6 +26,7 @@ package hudson;
import
hudson.FilePath.TarCompression
;
import
hudson.model.TaskListener
;
import
hudson.os.PosixAPI
;
import
hudson.os.WindowsUtil
;
import
hudson.remoting.VirtualChannel
;
import
hudson.util.NullStream
;
import
hudson.util.StreamTaskListener
;
...
...
@@ -836,7 +837,7 @@ public class FilePathTest {
Path
targetDir
=
temp
.
newFolder
(
"targetDir"
).
toPath
();
Path
targetContents
=
Files
.
createFile
(
targetDir
.
resolve
(
"contents.txt"
));
Path
toDelete
=
temp
.
newFolder
(
"toDelete"
).
toPath
();
File
junction
=
Util
.
createJunction
(
toDelete
.
resolve
(
"junction"
).
toFile
(),
targetDir
.
toFile
());
File
junction
=
Windows
Util
.
createJunction
(
toDelete
.
resolve
(
"junction"
).
toFile
(),
targetDir
.
toFile
());
Files
.
createFile
(
toDelete
.
resolve
(
"foo"
));
Files
.
createFile
(
toDelete
.
resolve
(
"bar"
));
FilePath
f
=
new
FilePath
(
toDelete
.
toFile
());
...
...
core/src/test/java/hudson/RemoveWindowsDirectoryJunctionTest.java
浏览文件 @
a489cc4e
...
...
@@ -8,6 +8,8 @@ import static org.junit.Assert.assertTrue;
import
static
org
.
junit
.
Assume
.
assumeTrue
;
import
java.io.File
;
import
hudson.os.WindowsUtil
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
...
...
@@ -32,7 +34,7 @@ public class RemoveWindowsDirectoryJunctionTest {
File
subdir1
=
tmp
.
newFolder
(
"notJunction"
);
File
f1
=
new
File
(
subdir1
,
"testfile1.txt"
);
assertTrue
(
"Unable to create temporary file in notJunction directory"
,
f1
.
createNewFile
());
File
j1
=
Util
.
createJunction
(
tmp
.
getRoot
(),
subdir1
);
File
j1
=
Windows
Util
.
createJunction
(
tmp
.
getRoot
(),
subdir1
);
Util
.
deleteRecursive
(
j1
);
assertFalse
(
"Windows Junction should have been removed"
,
j1
.
exists
());
assertTrue
(
"Contents of Windows Junction should not be removed"
,
f1
.
exists
());
...
...
core/src/test/java/hudson/UtilTest.java
浏览文件 @
a489cc4e
...
...
@@ -43,6 +43,7 @@ import static org.hamcrest.CoreMatchers.not;
import
static
org
.
hamcrest
.
CoreMatchers
.
startsWith
;
import
static
org
.
junit
.
Assert
.*;
import
hudson.os.WindowsUtil
;
import
org.hamcrest.BaseMatcher
;
import
org.hamcrest.Description
;
import
org.junit.Assume
;
...
...
@@ -262,7 +263,7 @@ public class UtilTest {
Assume
.
assumeTrue
(
"Uses Windows-specific features"
,
Functions
.
isWindows
());
File
targetDir
=
tmp
.
newFolder
(
"targetDir"
);
File
d
=
tmp
.
newFolder
(
"dir"
);
File
junction
=
Util
.
createJunction
(
new
File
(
d
,
"junction"
),
targetDir
);
File
junction
=
Windows
Util
.
createJunction
(
new
File
(
d
,
"junction"
),
targetDir
);
assertTrue
(
Util
.
isSymlink
(
junction
));
}
...
...
@@ -274,7 +275,7 @@ public class UtilTest {
File
file
=
new
File
(
targetDir
,
"test-file"
);
new
FilePath
(
file
).
touch
(
System
.
currentTimeMillis
());
File
dir
=
tmp
.
newFolder
();
File
junction
=
Util
.
createJunction
(
new
File
(
dir
,
"junction"
),
targetDir
);
File
junction
=
Windows
Util
.
createJunction
(
new
File
(
dir
,
"junction"
),
targetDir
);
assertTrue
(
Util
.
isSymlink
(
junction
));
assertFalse
(
Util
.
isSymlink
(
file
));
...
...
core/src/
main/java/jenkins/util/os/windows/WindowsCommandLineFormatter
.java
→
core/src/
test/java/hudson/os/WindowsUtil
.java
浏览文件 @
a489cc4e
...
...
@@ -22,24 +22,21 @@
* THE SOFTWARE.
*/
package
jenkins.util.os.window
s
;
package
hudson.o
s
;
import
org.kohsuke.accmod.Restricted
;
import
org.
kohsuke.accmod.restrictions.NoExternalUse
;
import
hudson.Functions
;
import
org.
apache.commons.io.IOUtils
;
import
javax.annotation.Nonnull
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.regex.Pattern
;
import
java.util.stream.Collectors
;
/**
* Provides formatting utilities for Windows command line processes.
*
* @see <a href="https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way">Everyone quotes command line arguments the wrong way</a>
*/
@Restricted
(
NoExternalUse
.
class
)
public
final
class
WindowsCommandLineFormatter
{
private
WindowsCommandLineFormatter
()
{
}
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
WindowsUtil
{
/**
* Quotes an argument while escaping special characters interpreted by CreateProcess.
*/
...
...
@@ -83,4 +80,33 @@ public final class WindowsCommandLineFormatter {
return
CMD_METACHARS
.
matcher
(
quoteArgument
(
argument
)).
replaceAll
(
"^$0"
);
}
/**
* Executes a command and arguments using {@code cmd.exe /C ...}.
*/
public
static
@Nonnull
Process
execCmd
(
String
...
argv
)
throws
IOException
{
String
command
=
Arrays
.
stream
(
argv
).
map
(
WindowsUtil:
:
quoteArgumentForCmd
).
collect
(
Collectors
.
joining
(
" "
));
return
Runtime
.
getRuntime
().
exec
(
new
String
[]{
"cmd.exe"
,
"/C"
,
command
});
}
/**
* Creates an NTFS junction point if supported. Similar to symbolic links, NTFS provides junction points which
* provide different features than symbolic links.
* @param junction NTFS junction point to create
* @param target target directory to junction
* @return the newly created junction point
* @throws IOException if the call to mklink exits with a non-zero status code
* @throws InterruptedException if the call to mklink is interrupted before completing
* @throws AssertionError if this method is called on a non-Windows platform
*/
public
static
@Nonnull
File
createJunction
(
@Nonnull
File
junction
,
@Nonnull
File
target
)
throws
IOException
,
InterruptedException
{
assertTrue
(
Functions
.
isWindows
());
Process
mklink
=
execCmd
(
"mklink"
,
"/J"
,
junction
.
getAbsolutePath
(),
target
.
getAbsolutePath
());
int
result
=
mklink
.
waitFor
();
if
(
result
!=
0
)
{
String
stderr
=
IOUtils
.
toString
(
mklink
.
getErrorStream
());
String
stdout
=
IOUtils
.
toString
(
mklink
.
getInputStream
());
throw
new
IOException
(
"Process exited with "
+
result
+
"\nStandard Output:\n"
+
stdout
+
"\nError Output:\n"
+
stderr
);
}
return
junction
;
}
}
core/src/test/java/
jenkins/util/os/windows/WindowsCommandLineFormatter
Test.java
→
core/src/test/java/
hudson/os/WindowsUtil
Test.java
浏览文件 @
a489cc4e
...
...
@@ -22,19 +22,21 @@
* THE SOFTWARE.
*/
package
jenkins.util.os.window
s
;
package
hudson.o
s
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
public
class
WindowsCommandLineFormatterTest
{
/**
* Test the test utility code, too!
*/
public
class
WindowsUtilTest
{
@Test
public
void
testQuoteArgument
()
{
String
input
=
"C:\\Programs and \"Settings\"\\System32\\\\"
;
String
expected
=
"\"C:\\Programs and \\\"Settings\\\"\\System32\\\\\\\\\""
;
String
actual
=
Windows
CommandLineFormatter
.
quoteArgument
(
input
);
String
actual
=
Windows
Util
.
quoteArgument
(
input
);
assertEquals
(
expected
,
actual
);
}
...
...
@@ -42,7 +44,7 @@ public class WindowsCommandLineFormatterTest {
public
void
testQuoteArgumentForCmd
()
{
String
input
=
"hello \"\\world&"
;
String
expected
=
"^\"hello \\^\"\\world^&^\""
;
String
actual
=
Windows
CommandLineFormatter
.
quoteArgumentForCmd
(
input
);
String
actual
=
Windows
Util
.
quoteArgumentForCmd
(
input
);
assertEquals
(
expected
,
actual
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录