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

Migrate complex file transfer specs to os-integration-test #223

Merged
merged 4 commits into from
Feb 16, 2017
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.hidetake.groovy.ssh.test.os

@Category(File)
class FileDivCategory {

File div(String child) {
new File(this as File, child)
}

File div(MkdirType type) {
switch (type) {
case MkdirType.DIRECTORY:
assert mkdir()
break

case MkdirType.DIRECTORIES:
assert mkdirs()
break

default:
throw new IllegalArgumentException("Unknown mkdir type: $type")
}
this
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.hidetake.groovy.ssh.test.os

enum MkdirType {
DIRECTORY,
DIRECTORIES
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.hidetake.groovy.ssh.test.os

import groovy.transform.Canonical
import groovy.util.logging.Slf4j
import org.hidetake.groovy.ssh.Ssh
import org.hidetake.groovy.ssh.core.Service
import org.hidetake.groovy.ssh.operation.SftpException
import org.junit.rules.ExternalResource

@Slf4j
class RemoteFixture extends ExternalResource {

private final Service ssh = Ssh.newService()

@Override
protected void before() {
Fixture.createRemotes(ssh)
}

@Override
protected void after() {
}

@Canonical
static class RemotePath {
Service ssh
String path

RemotePath div(String child) {
new RemotePath(ssh, "$path/$child")
}

RemotePath div(MkdirType type) {
switch (type) {
case MkdirType.DIRECTORY:
ssh.run {
session(ssh.remotes.Default) {
execute("mkdir $path")
}
}
break

case MkdirType.DIRECTORIES:
ssh.run {
session(ssh.remotes.Default) {
execute("mkdir -p $path")
}
}
break

default:
throw new IllegalArgumentException("Unknown mkdir type: $type")
}
this
}

String getName() {
path.substring(path.lastIndexOf('/'))
}

String getText() {
ssh.run {
session(ssh.remotes.Default) {
get from: path
}
}
}

RemotePath leftShift(String text) {
ssh.run {
session(ssh.remotes.Default) {
put text: text, into: path
}
}
this
}

List list() {
ssh.run {
session(ssh.remotes.Default) {
sftp {
ls(path).findAll { !(it.filename in ['.', '..']) }
}
}
} as List
}

boolean exists() {
try {
ssh.run {
session(ssh.remotes.Default) {
sftp {
stat(path)
}
}
} as List
true
} catch (SftpException e) {
false
}
}

@Override
String toString() {
path
}
}

RemotePath newFolder() {
def path = new RemotePath(ssh, Fixture.remoteTmpPath())
path / MkdirType.DIRECTORY
path
}

RemotePath newFile() {
def folder = newFolder()
folder / "${UUID.randomUUID()}"
}

}
Loading