-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a way to clean logs in container's log page #9528
Open
benoitf
wants to merge
1
commit into
containers:main
Choose a base branch
from
benoitf:DESKTOP-8311
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
packages/renderer/src/lib/container/ContainerDetailsLogs.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import '@testing-library/jest-dom/vitest'; | ||
|
||
import { render, screen } from '@testing-library/svelte'; | ||
import * as xterm from '@xterm/xterm'; | ||
import { beforeAll, expect, test, vi } from 'vitest'; | ||
|
||
import ContainerDetailsLogs from './ContainerDetailsLogs.svelte'; | ||
import type { ContainerInfoUI } from './ContainerInfoUI'; | ||
|
||
vi.mock('@xterm/xterm', () => { | ||
const writeMock = vi.fn(); | ||
return { | ||
writeMock, | ||
Terminal: vi.fn().mockReturnValue({ | ||
loadAddon: vi.fn(), | ||
open: vi.fn().mockImplementation((div: HTMLDivElement) => { | ||
// create a dummy div element | ||
const h = document.createElement('H1'); | ||
const t = document.createTextNode('dummy element'); | ||
h.appendChild(t); | ||
div.appendChild(h); | ||
}), | ||
write: writeMock, | ||
clear: vi.fn(), | ||
dispose: vi.fn(), | ||
}), | ||
}; | ||
}); | ||
|
||
beforeAll(() => { | ||
// Mock returned values with fake ones | ||
const mockComputedStyle = { | ||
getPropertyValue: vi.fn().mockReturnValue('#ffffff'), | ||
}; | ||
Object.defineProperty(global, 'window', { | ||
value: { | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
logsContainer: vi.fn(), | ||
getConfigurationValue: vi.fn(), | ||
getComputedStyle: vi.fn().mockReturnValue(mockComputedStyle), | ||
dispatchEvent: vi.fn(), | ||
}, | ||
writable: true, | ||
}); | ||
}); | ||
|
||
const container: ContainerInfoUI = { | ||
id: 'foo', | ||
} as unknown as ContainerInfoUI; | ||
|
||
test('Render container logs ', async () => { | ||
// grab the xterm mock | ||
let writeMock: unknown | undefined; | ||
if ('writeMock' in xterm) { | ||
writeMock = xterm.writeMock; | ||
} | ||
|
||
// Mock compose has no containers, so expect No Log to appear | ||
render(ContainerDetailsLogs, { container }); | ||
|
||
// expect a call to logsContainer | ||
await vi.waitFor(() => { | ||
expect(window.logsContainer).toHaveBeenCalled(); | ||
}); | ||
// now, get the callback of the method | ||
const params = vi.mocked(window.logsContainer).mock.calls[0][0]; | ||
// call the callback with an empty array | ||
params.callback('data', 'hello world'); | ||
|
||
// expect logs to have been called | ||
await vi.waitFor(() => { | ||
expect(vi.mocked(writeMock)).toHaveBeenCalled(); | ||
}); | ||
|
||
// expect the button to clear | ||
const clearButton = screen.getByRole('button', { name: 'Clear logs' }); | ||
expect(clearButton).toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/renderer/src/lib/container/ContainerDetailsLogsClear.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2024 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
|
||
import '@testing-library/jest-dom/vitest'; | ||
|
||
import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; | ||
import { Terminal } from '@xterm/xterm'; | ||
import { expect, test, vi } from 'vitest'; | ||
|
||
import ContainerDetailsLogsClear from './ContainerDetailsLogsClear.svelte'; | ||
|
||
vi.mock('@xterm/xterm', () => { | ||
const writeMock = vi.fn(); | ||
return { | ||
writeMock, | ||
Terminal: vi | ||
.fn() | ||
.mockReturnValue({ loadAddon: vi.fn(), open: vi.fn(), write: writeMock, clear: vi.fn(), dispose: vi.fn() }), | ||
}; | ||
}); | ||
|
||
test('expect clear button is working', async () => { | ||
const terminal = new Terminal(); | ||
|
||
render(ContainerDetailsLogsClear, { terminal }); | ||
|
||
// expect the button to clear | ||
const clearButton = screen.getByRole('button', { name: 'Clear logs' }); | ||
expect(clearButton).toBeInTheDocument(); | ||
|
||
// click the button | ||
await fireEvent.click(clearButton); | ||
|
||
// check we have called the clear function | ||
await waitFor(() => expect(terminal.clear).toHaveBeenCalled()); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/renderer/src/lib/container/ContainerDetailsLogsClear.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<script lang="ts"> | ||
import { faEraser } from '@fortawesome/free-solid-svg-icons'; | ||
import type { Terminal } from '@xterm/xterm'; | ||
import Fa from 'svelte-fa'; | ||
const { terminal }: { terminal: Terminal } = $props(); | ||
function clear(): void { | ||
terminal.clear(); | ||
} | ||
</script> | ||
|
||
<div class="absolute top-0 right-0 px-1 z-50 m-1 opacity-50 space-x-1"> | ||
<button title="Clear logs" onclick={clear}> | ||
<Fa | ||
class="cursor-pointer rounded-full bg-[var(--pd-button-disabled)] min-h-8 w-8 p-1.5" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing a hover effect. The icon isn't as bright as the actions on top, but if I remove the opacity it is too bright. I think it's missing a text- and regular opacity would help with the previous comment. |
||
icon={faEraser} | ||
/> | ||
</button> | ||
</div> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first Logs I went to I wouldn't have known the button was there if I wasn't looking. Maybe a little more opacity?
Likewise, it is over top of the mem tooltip. z-order should be lowered so that tooltips remain on top.