How to use Failf method of tool Package

Best Syzkaller code snippet using tool.Failf

main.go

Source:main.go Github

copy

Full Screen

1package main2import (3 "bufio"4 "fmt"5 "os"6 "path/filepath"7 "strings"8 "time"9 "github.com/bitrise-io/go-utils/fileutil"10 "github.com/bitrise-io/go-utils/log"11 "github.com/bitrise-io/go-utils/pathutil"12 "github.com/bitrise-tools/go-steputils/input"13 "github.com/bitrise-tools/go-steputils/tools"14 "github.com/bitrise-tools/go-xamarin/builder"15 "github.com/bitrise-tools/go-xamarin/constants"16 "github.com/bitrise-tools/go-xamarin/tools/buildtools"17 "github.com/bitrise-tools/go-xamarin/tools/nunit"18 "github.com/bitrise-tools/go-xcode/simulator"19 "github.com/hashicorp/go-version"20)21// ConfigsModel ...22type ConfigsModel struct {23 SimulatorDevice string24 SimulatorOsVersion string25 TestToRun string26 XamarinSolution string27 XamarinConfiguration string28 XamarinPlatform string29 BuildTool string30 DeployDir string31}32func createConfigsModelFromEnvs() ConfigsModel {33 return ConfigsModel{34 SimulatorDevice: os.Getenv("simulator_device"),35 SimulatorOsVersion: os.Getenv("simulator_os_version"),36 TestToRun: os.Getenv("test_to_run"),37 XamarinSolution: os.Getenv("xamarin_project"),38 XamarinConfiguration: os.Getenv("xamarin_configuration"),39 XamarinPlatform: os.Getenv("xamarin_platform"),40 BuildTool: os.Getenv("build_tool"),41 DeployDir: os.Getenv("BITRISE_DEPLOY_DIR"),42 }43}44func (configs ConfigsModel) print() {45 log.Infof("Testing:")46 log.Printf("- SimulatorDevice: %s", configs.SimulatorDevice)47 log.Printf("- SimulatorOsVersion: %s", configs.SimulatorOsVersion)48 log.Printf("- TestToRun: %s", configs.TestToRun)49 log.Infof("Configs:")50 log.Printf("- XamarinSolution: %s", configs.XamarinSolution)51 log.Printf("- XamarinConfiguration: %s", configs.XamarinConfiguration)52 log.Printf("- XamarinPlatform: %s", configs.XamarinPlatform)53 log.Infof("Debug:")54 log.Printf("- BuildTool: %s", configs.BuildTool)55 log.Printf("- DeployDir: %s", configs.DeployDir)56}57func (configs ConfigsModel) validate() error {58 if err := input.ValidateIfNotEmpty(configs.SimulatorDevice); err != nil {59 return fmt.Errorf("SimulatorDevice - %s", err)60 }61 if err := input.ValidateIfNotEmpty(configs.SimulatorOsVersion); err != nil {62 return fmt.Errorf("SimulatorOsVersion - %s", err)63 }64 if err := input.ValidateIfPathExists(configs.XamarinSolution); err != nil {65 return fmt.Errorf("XamarinSolution - %s", err)66 }67 if err := input.ValidateIfNotEmpty(configs.XamarinConfiguration); err != nil {68 return fmt.Errorf("XamarinConfiguration - %s", err)69 }70 if err := input.ValidateIfNotEmpty(configs.XamarinPlatform); err != nil {71 return fmt.Errorf("XamarinPlatform - %s", err)72 }73 if err := input.ValidateWithOptions(configs.BuildTool, "msbuild", "xbuild"); err != nil {74 return fmt.Errorf("BuildTool - %s", err)75 }76 return nil77}78func getLatestIOSVersion(osVersionSimulatorInfosMap simulator.OsVersionSimulatorInfosMap) (string, error) {79 var latestVersionPtr *version.Version80 for osVersion := range osVersionSimulatorInfosMap {81 if !strings.HasPrefix(osVersion, "iOS") {82 continue83 }84 versionStr := strings.TrimPrefix(osVersion, "iOS")85 versionStr = strings.TrimSpace(versionStr)86 versionPtr, err := version.NewVersion(versionStr)87 if err != nil {88 return "", fmt.Errorf("Failed to parse version (%s), error: %s", versionStr, err)89 }90 if latestVersionPtr == nil || versionPtr.GreaterThan(latestVersionPtr) {91 latestVersionPtr = versionPtr92 }93 }94 if latestVersionPtr == nil {95 return "", fmt.Errorf("Failed to determin latest iOS simulator version")96 }97 versionSegments := latestVersionPtr.Segments()98 if len(versionSegments) < 2 {99 return "", fmt.Errorf("Invalid version created: %s, segments count < 2", latestVersionPtr.String())100 }101 return fmt.Sprintf("iOS %d.%d", versionSegments[0], versionSegments[1]), nil102}103func getSimulatorInfo(osVersion, deviceName string) (simulator.InfoModel, error) {104 osVersionSimulatorInfosMap, err := simulator.GetOsVersionSimulatorInfosMap()105 if err != nil {106 return simulator.InfoModel{}, err107 }108 if osVersion == "latest" {109 latestOSVersion, err := getLatestIOSVersion(osVersionSimulatorInfosMap)110 if err != nil {111 return simulator.InfoModel{}, err112 }113 osVersion = latestOSVersion114 }115 infos, ok := osVersionSimulatorInfosMap[osVersion]116 if !ok {117 return simulator.InfoModel{}, fmt.Errorf("No simulators found for os version: %s", osVersion)118 }119 for _, info := range infos {120 if info.Name == deviceName {121 return info, nil122 }123 }124 return simulator.InfoModel{}, fmt.Errorf("No simulators found for os version: (%s), device name: (%s)", osVersion, deviceName)125}126func testResultLogContent(pth string) (string, error) {127 if exist, err := pathutil.IsPathExists(pth); err != nil {128 return "", fmt.Errorf("Failed to check if path (%s) exist, error: %s", pth, err)129 } else if !exist {130 return "", fmt.Errorf("test result not exist at: %s", pth)131 }132 content, err := fileutil.ReadStringFromFile(pth)133 if err != nil {134 return "", fmt.Errorf("Failed to read file (%s), error: %s", pth, err)135 }136 return content, nil137}138func parseErrorFromResultLog(content string) (string, error) {139 failureLineFound := false140 lastFailureMessage := ""141 scanner := bufio.NewScanner(strings.NewReader(content))142 for scanner.Scan() {143 line := strings.TrimSpace(scanner.Text())144 if line == "<failure>" {145 failureLineFound = true146 continue147 }148 if failureLineFound && strings.HasPrefix(line, "<message>") {149 lastFailureMessage = line150 }151 failureLineFound = false152 }153 return lastFailureMessage, nil154}155func failf(format string, v ...interface{}) {156 log.Errorf(format, v...)157 if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_RESULT", "failed"); err != nil {158 log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_RESULT", err)159 }160 os.Exit(1)161}162func main() {163 configs := createConfigsModelFromEnvs()164 fmt.Println()165 configs.print()166 if err := configs.validate(); err != nil {167 failf("Issue with input: %s", err)168 }169 // Get Simulator Infos170 fmt.Println()171 log.Infof("Collecting simulator info...")172 simulatorInfo, err := getSimulatorInfo(configs.SimulatorOsVersion, configs.SimulatorDevice)173 if err != nil {174 failf("Failed to get simulator infos, error: %s", err)175 }176 log.Donef("Simulator (%s), id: (%s), status: %s", simulatorInfo.Name, simulatorInfo.ID, simulatorInfo.Status)177 if err := os.Setenv("IOS_SIMULATOR_UDID", simulatorInfo.ID); err != nil {178 failf("Failed to export simulator UDID, error: %s", err)179 }180 // ---181 // Nunit Console path182 nunitConsolePth, err := nunit.SystemNunit3ConsolePath()183 if err != nil {184 failf("Failed to get system insatlled nunit3-console.exe path, error: %s", err)185 }186 // ---187 //188 // build189 fmt.Println()190 log.Infof("Building all iOS Xamarin UITest and Referred Projects in solution: %s", configs.XamarinSolution)191 buildTool := buildtools.Msbuild192 if configs.BuildTool == "xbuild" {193 buildTool = buildtools.Xbuild194 }195 builder, err := builder.New(configs.XamarinSolution, []constants.SDK{constants.SDKIOS}, buildTool)196 if err != nil {197 failf("Failed to create xamarin builder, error: %s", err)198 }199 callback := func(solutionName string, projectName string, sdk constants.SDK, testFramework constants.TestFramework, commandStr string, alreadyPerformed bool) {200 fmt.Println()201 if testFramework == constants.TestFrameworkXamarinUITest {202 log.Infof("Building test project: %s", projectName)203 } else {204 log.Infof("Building project: %s", projectName)205 }206 log.Donef("$ %s", commandStr)207 if alreadyPerformed {208 log.Warnf("build command already performed, skipping...")209 }210 fmt.Println()211 }212 startTime := time.Now()213 warnings, err := builder.BuildAndRunAllXamarinUITestAndReferredProjects(configs.XamarinConfiguration, configs.XamarinPlatform, nil, callback)214 endTime := time.Now()215 for _, warning := range warnings {216 log.Warnf(warning)217 }218 if err != nil {219 failf("Build failed, error: %s", err)220 }221 projectOutputMap, err := builder.CollectProjectOutputs(configs.XamarinConfiguration, configs.XamarinPlatform, startTime, endTime)222 if err != nil {223 failf("Failed to collect project outputs, error: %s", err)224 }225 testProjectOutputMap, warnings, err := builder.CollectXamarinUITestProjectOutputs(configs.XamarinConfiguration, configs.XamarinPlatform, startTime, endTime)226 for _, warning := range warnings {227 log.Warnf(warning)228 }229 if err != nil {230 failf("Failed to collect test project output, error: %s", err)231 }232 if len(testProjectOutputMap) == 0 {233 failf("No testable output generated")234 }235 // ---236 //237 // Run nunit tests238 nunitConsole, err := nunit.New(nunitConsolePth)239 if err != nil {240 failf("Failed to create nunit console model, error: %s", err)241 }242 resultLogPth := filepath.Join(configs.DeployDir, "TestResult.xml")243 nunitConsole.SetResultLogPth(resultLogPth)244 // Artifacts245 resultLog := ""246 for testProjectName, testProjectOutput := range testProjectOutputMap {247 if len(testProjectOutput.ReferredProjectNames) == 0 {248 log.Warnf("Test project (%s) does not refers to any project, skipping...", testProjectName)249 continue250 }251 for _, projectName := range testProjectOutput.ReferredProjectNames {252 projectOutput, ok := projectOutputMap[projectName]253 if !ok {254 continue255 }256 appPth := ""257 for _, output := range projectOutput.Outputs {258 if output.OutputType == constants.OutputTypeAPP {259 appPth = output.Pth260 }261 }262 if appPth == "" {263 failf("No app generated for project: %s", projectName)264 }265 // Set APP_BUNDLE_PATH env to let the test know which .app file should be tested266 // This env is used in the Xamarin.UITest project to refer to the .app path267 if err := os.Setenv("APP_BUNDLE_PATH", appPth); err != nil {268 failf("Failed to set APP_BUNDLE_PATH environment, without this env test will fail, error: %s", err)269 }270 // Run test271 fmt.Println()272 log.Infof("Testing (%s) against (%s)", testProjectName, projectName)273 log.Printf("test dll: %s", testProjectOutput.Output.Pth)274 log.Printf("app: %s", appPth)275 nunitConsole.SetDLLPth(testProjectOutput.Output.Pth)276 nunitConsole.SetTestToRun(configs.TestToRun)277 fmt.Println()278 log.Infof("Running Xamarin UITest")279 log.Donef("$ %s", nunitConsole.PrintableCommand())280 fmt.Println()281 err := nunitConsole.Run()282 testLog, readErr := testResultLogContent(resultLogPth)283 if readErr != nil {284 log.Warnf("Failed to read test result, error: %s", readErr)285 }286 resultLog = testLog287 if err != nil {288 if errorMsg, err := parseErrorFromResultLog(resultLog); err != nil {289 log.Warnf("Failed to parse error message from result log, error: %s", err)290 } else if errorMsg != "" {291 log.Errorf("%s", errorMsg)292 }293 if resultLog != "" {294 if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", resultLog); err != nil {295 log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", err)296 }297 }298 failf("Test failed, error: %s", err)299 }300 }301 }302 if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_RESULT", "succeeded"); err != nil {303 log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_RESULT", err)304 }305 if resultLog != "" {306 if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", resultLog); err != nil {307 log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", err)308 }309 }310}...

Full Screen

Full Screen

syz-db.go

Source:syz-db.go Github

copy

Full Screen

...34 usage()35 }36 target, err := prog.GetTarget(*flagOS, *flagArch)37 if err != nil {38 tool.Failf("failed to find target: %v", err)39 }40 bench(target, args[1])41 return42 }43 if len(args) != 3 {44 usage()45 }46 var target *prog.Target47 if *flagOS != "" || *flagArch != "" {48 var err error49 target, err = prog.GetTarget(*flagOS, *flagArch)50 if err != nil {51 tool.Failf("failed to find target: %v", err)52 }53 }54 switch args[0] {55 case "pack":56 pack(args[1], args[2], target, *flagVersion)57 case "unpack":58 unpack(args[1], args[2])59 default:60 usage()61 }62}63func usage() {64 fmt.Fprintf(os.Stderr, "usage:\n")65 fmt.Fprintf(os.Stderr, " syz-db pack dir corpus.db\n")66 fmt.Fprintf(os.Stderr, " syz-db unpack corpus.db dir\n")67 fmt.Fprintf(os.Stderr, " syz-db bench corpus.db\n")68 os.Exit(1)69}70func pack(dir, file string, target *prog.Target, version uint64) {71 files, err := ioutil.ReadDir(dir)72 if err != nil {73 tool.Failf("failed to read dir: %v", err)74 }75 var records []db.Record76 for _, file := range files {77 data, err := ioutil.ReadFile(filepath.Join(dir, file.Name()))78 if err != nil {79 tool.Failf("failed to read file %v: %v", file.Name(), err)80 }81 var seq uint6482 key := file.Name()83 if parts := strings.Split(file.Name(), "-"); len(parts) == 2 {84 var err error85 if seq, err = strconv.ParseUint(parts[1], 10, 64); err == nil {86 key = parts[0]87 }88 }89 if sig := hash.String(data); key != sig {90 if target != nil {91 p, err := target.Deserialize(data, prog.NonStrict)92 if err != nil {93 tool.Failf("failed to deserialize %v: %v", file.Name(), err)94 }95 data = p.Serialize()96 sig = hash.String(data)97 }98 fmt.Fprintf(os.Stderr, "fixing hash %v -> %v\n", key, sig)99 key = sig100 }101 records = append(records, db.Record{102 Val: data,103 Seq: seq,104 })105 }106 if err := db.Create(file, version, records); err != nil {107 tool.Fail(err)108 }109}110func unpack(file, dir string) {111 db, err := db.Open(file, false)112 if err != nil {113 tool.Failf("failed to open database: %v", err)114 }115 osutil.MkdirAll(dir)116 for key, rec := range db.Records {117 fname := filepath.Join(dir, key)118 if rec.Seq != 0 {119 fname += fmt.Sprintf("-%v", rec.Seq)120 }121 if err := osutil.WriteFile(fname, rec.Val); err != nil {122 tool.Failf("failed to output file: %v", err)123 }124 }125}126func bench(target *prog.Target, file string) {127 start := time.Now()128 db, err := db.Open(file, false)129 if err != nil {130 tool.Failf("failed to open database: %v", err)131 }132 var corpus []*prog.Prog133 for _, rec := range db.Records {134 p, err := target.Deserialize(rec.Val, prog.NonStrict)135 if err != nil {136 tool.Failf("failed to deserialize: %v\n%s", err, rec.Val)137 }138 corpus = append(corpus, p)139 }140 runtime.GC()141 var stats runtime.MemStats142 runtime.ReadMemStats(&stats)143 fmt.Printf("allocs %v MB (%v M), next GC %v MB, sys heap %v MB, live allocs %v MB (%v M), time %v\n",144 stats.TotalAlloc>>20,145 stats.Mallocs>>20,146 stats.NextGC>>20,147 stats.HeapSys>>20,148 stats.Alloc>>20,149 (stats.Mallocs-stats.Frees)>>20,150 time.Since(start))...

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import (2func TestFail(t *testing.T) {3 t.Fail()4}5func TestFailNow(t *testing.T) {6 t.FailNow()7}8func TestFatal(t *testing.T) {9 t.Fatal("Fatal")10}11func TestFatalf(t *testing.T) {12 t.Fatalf("Fatal %s", "message")13}

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import (2func Test_Failf(t *testing.T) {3 beego.Run()4 job := toolbox.NewTask("job", "0/1 * * * * *", func() error {5 beego.Info("I am running")6 })7 job.Run()8 beego.Info("I am running")9 job.Failf("I am failed")10 beego.Info("I am running")11 job.Fail()12 beego.Info("I am running")13}14import (15func Test_Lock(t *testing.T) {16 beego.Run()17 job := toolbox.NewTask("job", "0/1 * * * * *", func() error {18 beego.Info("I am running")19 })20 job.Run()21 beego.Info("I am running")22 job.Lock()23 beego.Info("I am

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import (2func TestFailf(t *testing.T) {3 t.Fail()4 fmt.Println("Fail test executed")5}6func TestSkipf(t *testing.T) {7 t.Skip()8 fmt.Println("Skip test executed")9}10func TestErrorf(t *testing.T) {11 t.Error()12 fmt.Println("Error test executed")13}14func TestLogf(t *testing.T) {15 t.Log()16 fmt.Println("Log test executed")17}18func TestHelper(t *testing.T) {19 t.Helper()20 fmt.Println("Helper test executed")21}22--- FAIL: TestFailf (0.00s)23--- SKIP: TestSkipf (0.00s)24--- FAIL: TestErrorf (0.00s)25--- PASS: TestLogf (0.00s)26--- PASS: TestHelper (0.00s

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1func main() {2 tool.Failf("error message")3}4--- FAIL: TestHello (0.00s)5testing.tRunner.func1(0xc4200b6000)6panic(0x4e2e60, 0x4f6c20)7main.TestHello(0xc4200b6000)8testing.tRunner(0xc4200b6000, 0x4f6c38)9created by testing.(*T).Run10FailNow() method11func main() {12 tool.FailNow()13}14--- FAIL: TestHello (0.00s)15testing.tRunner.func1(0xc4200b6000)16panic(0x4e2e60, 0x4f6c20)17main.TestHello(0xc4200b6000)18testing.tRunner(0xc4200b6000, 0x4f6c38)19created by testing.(*T).Run

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import (2func TestFailf(t *testing.T) {3 tool.Failf("test failf")4}5import (6func Failf(format string, args ...interface{}) {7 testing.Failf("FAIL", format, args...)8}9--- FAIL: TestFailf (0.00s)10--- FAIL: TestFailf (0.00s)

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4}5func TestFailf(t *testing.T) {6 t.Fail()7 t.FailNow()8 t.Fatalf("This is a fatal error")9}10import (11func main() {12 fmt.Println("Hello, playground")13}14func TestErrorf(t *testing.T) {15 t.Error()16 t.Errorf("This is a error")17}18import (19func main() {20 fmt.Println("Hello, playground")21}22func TestLogf(t *testing.T) {23 t.Log()24 t.Logf("This is a log")25}26import (27func main() {28 fmt.Println("Hello, playground")29}30func TestSkipf(t *testing.T) {31 t.Skip()32 t.SkipNow()33 t.Skipf("This is a skip")34}35import (36func main() {37 fmt.Println("Hello, playground")38}39func TestParallel(t *testing.T) {40 t.Parallel()41}

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 test.Failf("This is a fail message")4}5import (6func main() {7 test.Skipf("This is a skip message")8}9import (10func main() {11 test.SkipNowf("This is a skip message")12}13import (14func main() {15 test.Errorf("This is a error message")16}17import (18func main() {19 test.Fatal("This is a fatal message")20}21import (22func main() {23 test.FailNow("This is a fail message")24}25import (26func main() {27 test.Skip("This is a skip message")28}29import (30func main() {31 test.SkipNow("This is a skip message")32}33import (34func main() {35 test.Error("This is a error message")36}37import (38func main() {39 test.Fail("This is a fail message")40}41import (42func main() {43 test.Logf("This is a log message")44}45import (46func main() {47 test.Log("This is a log message")48}

Full Screen

Full Screen

Failf

Using AI Code Generation

copy

Full Screen

1import "testing"2func TestSum(t *testing.T) {3 t.Failf("Sum function is not working properly", "Sum(1, 2) should be 3 but got %d", Sum(1, 2))4}5--- FAIL: TestSum (0.00s)6 Sum(1, 2) should be 3 but got 07FailNow()8import "testing"9func TestSum(t *testing.T) {10 t.FailNow()11 t.Log("This will not be printed")12}13--- FAIL: TestSum (0.00s)14Skip()15import "testing"16func TestSum(t *testing.T) {17 t.Skip("Skipping the test function")18 t.Log("This will not be printed")19}20--- SKIP: TestSum (0.00s)21SkipNow()22import "testing"23func TestSum(t *testing.T) {24 t.SkipNow()25 t.Log("This will not be printed")26}27--- SKIP: TestSum (0.00s)28Skipf()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful