1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package vogar;
18
19import com.google.common.collect.ImmutableList;
20import java.io.File;
21import java.util.Arrays;
22import java.util.Collections;
23import java.util.List;
24import vogar.commands.Command;
25
26/**
27 * Run tests on the host machine.
28 */
29public final class LocalTarget extends Target {
30    private final Run run;
31
32    public LocalTarget(Run run) {
33        this.run = run;
34    }
35
36    @Override public File defaultDeviceDir() {
37        return new File("/tmp/vogar");
38    }
39
40    @Override public List<String> targetProcessPrefix() {
41        return ImmutableList.of("sh", "-c");
42    }
43
44    @Override public String getDeviceUserName() {
45        throw new UnsupportedOperationException();
46    }
47
48    @Override public void await(File nonEmptyDirectory) {
49    }
50
51    @Override public void rm(File file) {
52        run.rm.file(file);
53    }
54
55    @Override public List<File> ls(File directory) {
56        return Arrays.asList(directory.listFiles());
57    }
58
59    @Override public void mkdirs(File file) {
60        run.mkdir.mkdirs(file);
61    }
62
63    @Override public void forwardTcp(int port) {
64        // do nothing
65    }
66
67    @Override public void push(File local, File remote) {
68        if (remote.equals(local)) {
69            return;
70        }
71        // if the user dir exists, cp would copy the files to the wrong place
72        if (remote.exists()) {
73            throw new IllegalStateException();
74        }
75        new Command(run.log, "cp", "-r", local.toString(), remote.toString()).execute();
76    }
77
78    @Override public void pull(File remote, File local) {
79        new Command(run.log, "cp", remote.getPath(), local.getPath()).execute();
80    }
81}
82