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