Skip to content

Commit

Permalink
Merge pull request #223 from int128/refactor-spec
Browse files Browse the repository at this point in the history
Migrate complex file transfer specs to os-integration-test
  • Loading branch information
int128 committed Feb 16, 2017
2 parents 9bd217f + bef3bc3 commit a181297
Show file tree
Hide file tree
Showing 8 changed files with 968 additions and 971 deletions.
@@ -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
}

}
@@ -0,0 +1,6 @@
package org.hidetake.groovy.ssh.test.os

enum MkdirType {
DIRECTORY,
DIRECTORIES
}
@@ -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()}"
}

}

0 comments on commit a181297

Please sign in to comment.