1 /* 2 * Copyright (C) 2022 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 package com.android.microdroid.test.host; 17 18 import static com.google.common.truth.Truth.assertAbout; 19 20 import com.android.tradefed.util.CommandResult; 21 import com.android.tradefed.util.CommandStatus; 22 23 import com.google.common.truth.FailureMetadata; 24 import com.google.common.truth.IntegerSubject; 25 import com.google.common.truth.StringSubject; 26 import com.google.common.truth.Subject; 27 28 /** 29 * A <a href="https://github.com/google/truth">Truth</a> subject for {@link CommandResult}. 30 */ 31 public class CommandResultSubject extends Subject { 32 private final CommandResult mActual; 33 command_results()34 public static Factory<CommandResultSubject, CommandResult> command_results() { 35 return CommandResultSubject::new; 36 } 37 assertThat(CommandResult actual)38 public static CommandResultSubject assertThat(CommandResult actual) { 39 return assertAbout(command_results()).that(actual); 40 } 41 CommandResultSubject(FailureMetadata metadata, CommandResult actual)42 private CommandResultSubject(FailureMetadata metadata, CommandResult actual) { 43 super(metadata, actual); 44 this.mActual = actual; 45 } 46 isSuccess()47 public void isSuccess() { 48 check("isSuccess()").that(mActual.getStatus()).isEqualTo(CommandStatus.SUCCESS); 49 } 50 isFailed()51 public void isFailed() { 52 check("isFailed()").that(mActual.getStatus()).isEqualTo(CommandStatus.FAILED); 53 } 54 isTimedOut()55 public void isTimedOut() { 56 check("isTimedOut()").that(mActual.getStatus()).isEqualTo(CommandStatus.TIMED_OUT); 57 } 58 isException()59 public void isException() { 60 check("isException()").that(mActual.getStatus()).isEqualTo(CommandStatus.EXCEPTION); 61 } 62 exitCode()63 public IntegerSubject exitCode() { 64 return check("exitCode()").that(mActual.getExitCode()); 65 } 66 stdoutTrimmed()67 public StringSubject stdoutTrimmed() { 68 return check("stdout()").that(mActual.getStdout().trim()); 69 } 70 stderrTrimmed()71 public StringSubject stderrTrimmed() { 72 return check("stderr()").that(mActual.getStderr().trim()); 73 } 74 } 75