1+ import { createBazelBuildAspectCommand } from '../../compilationDatabase' ;
2+ import * as assert from 'assert' ;
3+ import * as fs from 'fs' ;
4+ import * as tmp from 'tmp' ;
5+ import path = require( 'path' ) ;
6+ import { ExecException , execFile } from 'child_process' ;
7+ import { assertType } from 'vscode-common/out/types' ;
8+
9+ suite ( 'Compilation Database' , ( ) => {
10+
11+ test ( 'createBazelBuildAspectCommand' , ( ) => {
12+ const repoPath = '/path/to/this/extension/compdb' ;
13+ const tmpFile = '/tmp/build-events.json' ;
14+ const buildArgs : string [ ] = [ "--config=foo" ] ;
15+ const targets : string [ ] = [ '//app/a' , '//app/b' ] ;
16+
17+ const cmd = createBazelBuildAspectCommand (
18+ repoPath ,
19+ tmpFile ,
20+ buildArgs ,
21+ targets ,
22+ ) ;
23+
24+ assert . deepStrictEqual ( [
25+ 'build' ,
26+ '--override_repository=bazel_vscode_compdb=/path/to/this/extension/compdb' ,
27+ '--aspects=@bazel_vscode_compdb//:aspects.bzl%compilation_database_aspect' ,
28+ '--color=no' ,
29+ '--noshow_progress' ,
30+ '--noshow_loading_progress' ,
31+ '--output_groups=compdb_files,header_files' ,
32+ '--build_event_json_file=/tmp/build-events.json' ,
33+ '--config=foo' ,
34+ '//app/a' ,
35+ '//app/b' ,
36+ '&&' ,
37+ process . platform === 'win32' ? '\\path\\to\\this\\extension\\compdb\\postprocess.py' : '/path/to/this/extension/compdb/postprocess.py' ,
38+ '-b' ,
39+ '/tmp/build-events.json' ,
40+ '&&' ,
41+ 'rm' ,
42+ '/tmp/build-events.json' ,
43+ ] , cmd ) ;
44+ } ) ;
45+
46+ // test not working in CI yet.
47+ test . skip ( 'postprocess.py exists' , ( done ) => {
48+ const extensionDevelopmentPath = path . resolve ( __dirname , '../../..' ) ;
49+ const postprocessPy = path . join ( extensionDevelopmentPath , 'compdb/postprocess.py' ) ;
50+ assert . ok ( fs . existsSync ( postprocessPy ) ) ;
51+
52+ // prep temp files
53+ const tmpDir = tmp . dirSync ( ) . name ;
54+ const buildEventsJsonTempFile = path . join ( tmpDir , 'build-events.json' ) ;
55+ const fooCommandsFile = path . join ( tmpDir , 'foo.compile_commands.json' ) ;
56+ const barCommandsFile = path . join ( tmpDir , 'bar.compile_commands.json' ) ;
57+ const compileCommandsFile = path . join ( tmpDir , 'compile_commands.json' ) ;
58+
59+ fs . writeFileSync ( fooCommandsFile , JSON . stringify ( [
60+ {
61+ command : "external/local_config_cc/cc_wrapper.sh -iquote . app/a/foo.cpp" ,
62+ directory : "__EXEC_ROOT__" ,
63+ file : "app/a/foo.cpp" ,
64+ } ,
65+ ] ) ) ;
66+ fs . writeFileSync ( barCommandsFile , JSON . stringify ( [
67+ {
68+ command : "external/local_config_cc/cc_wrapper.sh -iquote . app/a/bar.cpp" ,
69+ directory : "__EXEC_ROOT__" ,
70+ file : "app/a/bar.cpp" ,
71+ } ,
72+ ] ) ) ;
73+
74+ // write fake build events as a file where every line is a JSON object
75+ const events = [
76+ JSON . stringify ( {
77+ started : {
78+ workspaceDirectory : tmpDir ,
79+ } ,
80+ } ) ,
81+ JSON . stringify ( {
82+ workspaceInfo : {
83+ localExecRoot : tmpDir ,
84+ } ,
85+ } ) ,
86+ JSON . stringify ( {
87+ progress : {
88+ stderr : [
89+ ' ' + fooCommandsFile ,
90+ ' ' + barCommandsFile ,
91+ ] . join ( '\n' ) ,
92+ } ,
93+ } ) ,
94+ ] ;
95+
96+ fs . writeFileSync ( buildEventsJsonTempFile , events . join ( '\n' ) ) ;
97+ const postprocessArgs = [ '--build_events_json_file' , buildEventsJsonTempFile ] ;
98+
99+ execFile ( postprocessPy , postprocessArgs , { } , ( err : ExecException | null , stdout : string | Buffer , stderr : string | Buffer ) => {
100+ if ( err ) {
101+ done ( err ) ;
102+ return ;
103+ }
104+ console . log ( 'OUT' , stdout ) ;
105+ console . log ( 'ERR' , stderr ) ;
106+ assert . ok ( fs . existsSync ( compileCommandsFile ) ) ;
107+
108+ const jsonContent = fs . readFileSync ( compileCommandsFile ) . toString ( )
109+ . split ( tmpDir ) . join ( '__TMPDIR__' ) ;
110+ const compileCommands = JSON . parse ( jsonContent ) ;
111+
112+ assert . deepStrictEqual ( compileCommands ,
113+ [
114+ {
115+ command : 'external/local_config_cc/cc_wrapper.sh -I . app/a/foo.cpp' ,
116+ directory : '__TMPDIR__' ,
117+ file : 'app/a/foo.cpp'
118+ } ,
119+ {
120+ command : 'external/local_config_cc/cc_wrapper.sh -I . app/a/bar.cpp' ,
121+ directory : '__TMPDIR__' ,
122+ file : 'app/a/bar.cpp'
123+ }
124+ ]
125+ ) ;
126+
127+ done ( ) ;
128+ } ) ;
129+
130+ } ) ;
131+
132+ } ) ;
0 commit comments