Skip to content

Commit 97937ec

Browse files
committed
add unit tests
1 parent 7d54dcc commit 97937ec

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/index.spec.ts

+62
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { TraceSource } from "./trace/trace-context-service";
2424
import { inflateSync } from "zlib";
2525
import { MetricsListener } from "./metrics/listener";
2626
import { SpanOptions, TracerWrapper } from "./trace/tracer-wrapper";
27+
import fs from "fs";
2728

2829
jest.mock("./metrics/enhanced-metrics");
2930

@@ -623,3 +624,64 @@ describe("emitTelemetryOnErrorOutsideHandler", () => {
623624
expect(mockedStartSpan).toBeCalledTimes(0);
624625
});
625626
});
627+
628+
describe("detectDuplicateInstallations", () => {
629+
jest.mock("fs");
630+
631+
let fsAccessMock: jest.SpyInstance;
632+
let logWarningMock: jest.SpyInstance;
633+
634+
beforeEach(() => {
635+
jest.resetAllMocks();
636+
fsAccessMock = jest.spyOn(fs, "access");
637+
logWarningMock = jest.spyOn(require("./utils"), "logWarning").mockImplementation(() => {});
638+
});
639+
640+
it("should log warning when duplicate installations are detected", async () => {
641+
// Mock fs.access to simulate both paths exist
642+
fsAccessMock.mockImplementation((path: string, callback: any) => {
643+
callback(null); // No error = path exists
644+
});
645+
646+
await datadog(async () => {}, { forceWrap: true })();
647+
expect(logWarningMock).toHaveBeenCalledWith(expect.stringContaining("Detected duplicate installations"));
648+
});
649+
650+
it("should not log warning when only layer installation exists", async () => {
651+
// Simulate layerPath exists, localPath does not exist
652+
fsAccessMock.mockImplementation((path: string, callback: any) => {
653+
if (path.includes("/opt/nodejs")) {
654+
callback(null); // Exists
655+
} else {
656+
callback(new Error("ENOENT")); // Does not exist
657+
}
658+
});
659+
660+
await datadog(async () => {}, { forceWrap: true })();
661+
expect(logWarningMock).not.toHaveBeenCalled();
662+
});
663+
664+
it("should not log warning when only local installation exists", async () => {
665+
// Simulate localPath exists, layerPath does not exist
666+
fsAccessMock.mockImplementation((path: string, callback: any) => {
667+
if (path.includes("/opt/nodejs")) {
668+
callback(new Error("ENOENT")); // Does not exist
669+
} else {
670+
callback(null); // Exists
671+
}
672+
});
673+
674+
await datadog(async () => {}, { forceWrap: true })();
675+
expect(logWarningMock).not.toHaveBeenCalled();
676+
});
677+
678+
it("should not log warning when neither installation exists", async () => {
679+
// Simulate neither path exists
680+
fsAccessMock.mockImplementation((path: string, callback: any) => {
681+
callback(new Error("ENOENT")); // Does not exist
682+
});
683+
684+
await datadog(async () => {}, { forceWrap: true })();
685+
expect(logWarningMock).not.toHaveBeenCalled();
686+
});
687+
});

0 commit comments

Comments
 (0)