123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- package cquery
- import (
- "encoding/json"
- "reflect"
- "strings"
- "testing"
- )
- func TestGetOutputFilesParseResults(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- input string
- expectedOutput []string
- }{
- {
- description: "no result",
- input: "",
- expectedOutput: []string{},
- },
- {
- description: "one result",
- input: "test",
- expectedOutput: []string{"test"},
- },
- {
- description: "splits on comma with space",
- input: "foo, bar",
- expectedOutput: []string{"foo", "bar"},
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- actualOutput := GetOutputFiles.ParseResult(tc.input)
- if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
- t.Errorf("expected %#v != actual %#v", tc.expectedOutput, actualOutput)
- }
- })
- }
- }
- func TestGetCcInfoParseResults(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- inputCcInfo CcInfo
- expectedOutput CcInfo
- }{
- {
- description: "no result",
- inputCcInfo: CcInfo{},
- expectedOutput: CcInfo{},
- },
- {
- description: "all items set",
- inputCcInfo: CcInfo{
- OutputFiles: []string{"out1", "out2"},
- CcObjectFiles: []string{"object1", "object2"},
- CcSharedLibraryFiles: []string{"shared_lib1", "shared_lib2"},
- CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"},
- Includes: []string{".", "dir/subdir"},
- SystemIncludes: []string{"system/dir", "system/other/dir"},
- Headers: []string{"dir/subdir/hdr.h"},
- RootStaticArchives: []string{"rootstaticarchive1"},
- RootDynamicLibraries: []string{"rootdynamiclibrary1"},
- TocFile: "lib.so.toc",
- },
- expectedOutput: CcInfo{
- OutputFiles: []string{"out1", "out2"},
- CcObjectFiles: []string{"object1", "object2"},
- CcSharedLibraryFiles: []string{"shared_lib1", "shared_lib2"},
- CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"},
- Includes: []string{".", "dir/subdir"},
- SystemIncludes: []string{"system/dir", "system/other/dir"},
- Headers: []string{"dir/subdir/hdr.h"},
- RootStaticArchives: []string{"rootstaticarchive1"},
- RootDynamicLibraries: []string{"rootdynamiclibrary1"},
- TocFile: "lib.so.toc",
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- jsonInput, _ := json.Marshal(tc.inputCcInfo)
- actualOutput, err := GetCcInfo.ParseResult(string(jsonInput))
- if err != nil {
- t.Errorf("error parsing result: %q", err)
- } else if err == nil && !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
- t.Errorf("expected %#v\n!= actual %#v", tc.expectedOutput, actualOutput)
- }
- })
- }
- }
- func TestGetCcInfoParseResultsError(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- input string
- expectedError string
- }{
- {
- description: "not json",
- input: ``,
- expectedError: `cannot parse cquery result '': EOF`,
- },
- {
- description: "invalid field",
- input: `{
- "toc_file": "dir/file.so.toc"
- }`,
- expectedError: `json: unknown field "toc_file"`,
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- _, err := GetCcInfo.ParseResult(tc.input)
- if !strings.Contains(err.Error(), tc.expectedError) {
- t.Errorf("expected string %q in error message, got %q", tc.expectedError, err)
- }
- })
- }
- }
- func TestGetApexInfoParseResults(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- input string
- expectedOutput ApexInfo
- }{
- {
- description: "no result",
- input: "{}",
- expectedOutput: ApexInfo{},
- },
- {
- description: "one result",
- input: `{
- "signed_output":"my.apex",
- "unsigned_output":"my.apex.unsigned",
- "requires_native_libs":["//bionic/libc:libc","//bionic/libdl:libdl"],
- "bundle_key_info":["foo.pem", "foo.privkey"],
- "container_key_info":["foo.x509.pem", "foo.pk8", "foo"],
- "package_name":"package.name",
- "symbols_used_by_apex": "path/to/my.apex_using.txt",
- "backing_libs":"path/to/backing.txt",
- "bundle_file": "dir/bundlefile.zip",
- "installed_files":"path/to/installed-files.txt",
- "provides_native_libs":[],
- "make_modules_to_install": ["foo","bar"]
- }`,
- expectedOutput: ApexInfo{
- // ApexInfo
- SignedOutput: "my.apex",
- UnsignedOutput: "my.apex.unsigned",
- RequiresLibs: []string{"//bionic/libc:libc", "//bionic/libdl:libdl"},
- ProvidesLibs: []string{},
- BundleKeyInfo: []string{"foo.pem", "foo.privkey"},
- ContainerKeyInfo: []string{"foo.x509.pem", "foo.pk8", "foo"},
- PackageName: "package.name",
- SymbolsUsedByApex: "path/to/my.apex_using.txt",
- BackingLibs: "path/to/backing.txt",
- BundleFile: "dir/bundlefile.zip",
- InstalledFiles: "path/to/installed-files.txt",
- // ApexMkInfo
- MakeModulesToInstall: []string{"foo", "bar"},
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- actualOutput, err := GetApexInfo.ParseResult(tc.input)
- if err != nil {
- t.Errorf("Unexpected error %q", err)
- }
- if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
- t.Errorf("expected %#v != actual %#v", tc.expectedOutput, actualOutput)
- }
- })
- }
- }
- func TestGetApexInfoParseResultsError(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- input string
- expectedError string
- }{
- {
- description: "not json",
- input: ``,
- expectedError: `cannot parse cquery result '': EOF`,
- },
- {
- description: "invalid field",
- input: `{
- "fake_field": "path/to/file"
- }`,
- expectedError: `json: unknown field "fake_field"`,
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- _, err := GetApexInfo.ParseResult(tc.input)
- if !strings.Contains(err.Error(), tc.expectedError) {
- t.Errorf("expected string %q in error message, got %q", tc.expectedError, err)
- }
- })
- }
- }
- func TestGetCcUnstrippedParseResults(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- input string
- expectedOutput CcUnstrippedInfo
- }{
- {
- description: "no result",
- input: "{}",
- expectedOutput: CcUnstrippedInfo{},
- },
- {
- description: "one result",
- input: `{"OutputFile":"myapp", "UnstrippedOutput":"myapp_unstripped"}`,
- expectedOutput: CcUnstrippedInfo{
- OutputFile: "myapp",
- UnstrippedOutput: "myapp_unstripped",
- },
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- actualOutput, err := GetCcUnstrippedInfo.ParseResult(tc.input)
- if err != nil {
- t.Errorf("Unexpected error %q", err)
- }
- if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
- t.Errorf("expected %#v != actual %#v", tc.expectedOutput, actualOutput)
- }
- })
- }
- }
- func TestGetCcUnstrippedParseResultsErrors(t *testing.T) {
- t.Parallel()
- testCases := []struct {
- description string
- input string
- expectedError string
- }{
- {
- description: "not json",
- input: ``,
- expectedError: `cannot parse cquery result '': EOF`,
- },
- {
- description: "invalid field",
- input: `{
- "fake_field": "path/to/file"
- }`,
- expectedError: `json: unknown field "fake_field"`,
- },
- }
- for _, tc := range testCases {
- t.Run(tc.description, func(t *testing.T) {
- _, err := GetCcUnstrippedInfo.ParseResult(tc.input)
- if !strings.Contains(err.Error(), tc.expectedError) {
- t.Errorf("expected string %q in error message, got %q", tc.expectedError, err)
- }
- })
- }
- }
|