@@ -24,6 +24,7 @@ describe('Config', () => {
2424 const BLOCKED_TEMPLATE_HTML = readFileSync ( BLOCKED_TEMPLATE_HTML_PATH , { encoding : 'utf8' } )
2525 const BLOCKED_TEMPLATE_JSON_PATH = require . resolve ( './fixtures/config/appsec-blocked-template.json' )
2626 const BLOCKED_TEMPLATE_JSON = readFileSync ( BLOCKED_TEMPLATE_JSON_PATH , { encoding : 'utf8' } )
27+ const DD_GIT_PROPERTIES_FILE = require . resolve ( './fixtures/config/git.properties' )
2728
2829 beforeEach ( ( ) => {
2930 pkg = {
@@ -936,7 +937,7 @@ describe('Config', () => {
936937 }
937938 } )
938939
939- expect ( log . error ) . to . be . calledThrice
940+ expect ( log . error ) . to . be . callCount ( 4 )
940941 expect ( log . error . firstCall ) . to . have . been . calledWithExactly ( error )
941942 expect ( log . error . secondCall ) . to . have . been . calledWithExactly ( error )
942943 expect ( log . error . thirdCall ) . to . have . been . calledWithExactly ( error )
@@ -1076,4 +1077,73 @@ describe('Config', () => {
10761077 } )
10771078 } )
10781079 } )
1080+
1081+ context ( 'sci embedding' , ( ) => {
1082+ const DUMMY_COMMIT_SHA = 'b7b5dfa992008c77ab3f8a10eb8711e0092445b0'
1083+ const DUMMY_REPOSITORY_URL = '[email protected] :DataDog/dd-trace-js.git' 1084+ let ddTags
1085+ beforeEach ( ( ) => {
1086+ ddTags = process . env . DD_TAGS
1087+ } )
1088+ afterEach ( ( ) => {
1089+ delete process . env . DD_GIT_PROPERTIES_FILE
1090+ delete process . env . DD_GIT_COMMIT_SHA
1091+ delete process . env . DD_GIT_REPOSITORY_URL
1092+ delete process . env . DD_TRACE_GIT_METADATA_ENABLED
1093+ process . env . DD_TAGS = ddTags
1094+ } )
1095+ it ( 'reads DD_GIT_* env vars' , ( ) => {
1096+ process . env . DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1097+ process . env . DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL
1098+ const config = new Config ( { } )
1099+ expect ( config ) . to . have . property ( 'commitSHA' , DUMMY_COMMIT_SHA )
1100+ expect ( config ) . to . have . property ( 'repositoryUrl' , DUMMY_REPOSITORY_URL )
1101+ } )
1102+ it ( 'reads DD_TAGS env var' , ( ) => {
1103+ process . env . DD_TAGS = `git.commit.sha:${ DUMMY_COMMIT_SHA } ,git.repository_url:${ DUMMY_REPOSITORY_URL } `
1104+ process . env . DD_GIT_REPOSITORY_URL = DUMMY_REPOSITORY_URL
1105+ const config = new Config ( { } )
1106+ expect ( config ) . to . have . property ( 'commitSHA' , DUMMY_COMMIT_SHA )
1107+ expect ( config ) . to . have . property ( 'repositoryUrl' , DUMMY_REPOSITORY_URL )
1108+ } )
1109+ it ( 'reads git.properties if it is available' , ( ) => {
1110+ process . env . DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1111+ const config = new Config ( { } )
1112+ expect ( config ) . to . have . property ( 'commitSHA' , '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d' )
1113+ expect ( config ) . to . have . property ( 'repositoryUrl' , DUMMY_REPOSITORY_URL )
1114+ } )
1115+ it ( 'does not crash if git.properties is not available' , ( ) => {
1116+ process . env . DD_GIT_PROPERTIES_FILE = '/does/not/exist'
1117+ const config = new Config ( { } )
1118+ expect ( config ) . to . have . property ( 'commitSHA' , undefined )
1119+ expect ( config ) . to . have . property ( 'repositoryUrl' , undefined )
1120+ } )
1121+ it ( 'does not read git.properties if env vars are passed' , ( ) => {
1122+ process . env . DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1123+ process . env . DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1124+ process . env . DD_GIT_REPOSITORY_URL = 'https://github.com:env-var/dd-trace-js.git'
1125+ const config = new Config ( { } )
1126+ expect ( config ) . to . have . property ( 'commitSHA' , DUMMY_COMMIT_SHA )
1127+ expect ( config ) . to . have . property ( 'repositoryUrl' , 'https://github.com:env-var/dd-trace-js.git' )
1128+ } )
1129+ it ( 'still reads git.properties if one of the env vars is missing' , ( ) => {
1130+ process . env . DD_GIT_PROPERTIES_FILE = DD_GIT_PROPERTIES_FILE
1131+ process . env . DD_GIT_COMMIT_SHA = DUMMY_COMMIT_SHA
1132+ const config = new Config ( { } )
1133+ expect ( config ) . to . have . property ( 'commitSHA' , DUMMY_COMMIT_SHA )
1134+ expect ( config ) . to . have . property ( 'repositoryUrl' , DUMMY_REPOSITORY_URL )
1135+ } )
1136+ it ( 'reads git.properties and filters out credentials' , ( ) => {
1137+ process . env . DD_GIT_PROPERTIES_FILE = require . resolve ( './fixtures/config/git.properties.credentials' )
1138+ const config = new Config ( { } )
1139+ expect ( config ) . to . have . property ( 'commitSHA' , '4e7da8069bcf5ffc8023603b95653e2dc99d1c7d' )
1140+ expect ( config ) . to . have . property ( 'repositoryUrl' , 'https://github.com/datadog/dd-trace-js' )
1141+ } )
1142+ it ( 'does not read git metadata if DD_TRACE_GIT_METADATA_ENABLED is false' , ( ) => {
1143+ process . env . DD_TRACE_GIT_METADATA_ENABLED = 'false'
1144+ const config = new Config ( { } )
1145+ expect ( config ) . not . to . have . property ( 'commitSHA' )
1146+ expect ( config ) . not . to . have . property ( 'repositoryUrl' )
1147+ } )
1148+ } )
10791149} )
0 commit comments