Skip to content
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

snap_compare failures using DirectoryTrees and .mount in spite of identical appearance #5073

Closed
mitaa opened this issue Sep 29, 2024 · 6 comments

Comments

@mitaa
Copy link

mitaa commented Sep 29, 2024

The MRE below always generates the same appearance in the terminal and snapshot_report.html,
but creates non-identical svg files leading to snap_compare failures (well sometimes, sometimes not).

Seemingly since textual 0.80

I've created a small git repo with the MRE and svg test samples here:
https://github.com/mitaa/mre_textual_svg_dtree

After taking 20 samples with each version i get the following

(filename = hash)

├───0.79.0
│       103ed17be2726a686e7b85e77fa2ab03af85c344aeb012985cda803a8147403d.svg
│
├───0.80.0
│       66ba2bb8610dfac2b1e8ad668ef79dd7b5e2636713fb586f4350e744ba0c2648.svg
│       8cbe0a12044fdeb2c5d763cc079c816d65ab9fda51764a1c498e1f8b9e74414b.svg
│
└───0.81.0
        2769b26e6544461cc1d3d5c6344a64b9353f7e6ef6fcd80417dfb929dfa79089.svg
        924cf504886288b5587dc86f2e517aaa5e8d89e7abd320f72a6afef013ecb8d9.svg

The difference within the svg files seem to be the UIDs (.terminal-) here
2769b26e6544461cc1d3d5c6344a64b9353f7e6ef6fcd80417dfb929dfa79089.svg

    .terminal-2464235620-matrix {
        font-family: Fira Code, monospace;
        font-size: 20px;
        line-height: 24.4px;
        font-variant-east-asian: full-width;
    }

924cf504886288b5587dc86f2e517aaa5e8d89e7abd320f72a6afef013ecb8d9.svg

    .terminal-2683650149-matrix {
        font-family: Fira Code, monospace;
        font-size: 20px;
        line-height: 24.4px;
        font-variant-east-asian: full-width;
    }

MRE code from the repo:
mre.py

from textual.app import App
from textual.widgets import DirectoryTree

ROOT = r"foo"


class MyApp(App):
    # This works
    # def compose(self):
    #     yield DirectoryTree(ROOT)
    #     yield DirectoryTree(ROOT)

    # This doesn't
    def on_mount(self) -> None:
        self.mount(DirectoryTree(ROOT))
        self.mount(DirectoryTree(ROOT))

    def on_tree_node_highlighted(self, node):
        self.query(DirectoryTree)[-1].reload()


def main():
    app = MyApp()
    app.app.run()


if __name__ == "__main__":
    main()

sample.py

import os
from pathlib import Path
from hashlib import sha256
import shutil

import textual
from textual._doc import take_svg_screenshot

from mre import MyApp


def main():
    root = Path("test_results") / textual.__version__
    os.makedirs(root, exist_ok=True)

    for _ in range(20):
        app = MyApp()
        actual = take_svg_screenshot(app).encode("utf-8")

        fpath = root / f"{sha256(actual).hexdigest()}.svg"
        fpath.write_bytes(actual)

if __name__ == "__main__":
    main()

Textual Diagnostics

Versions

Name Value
Textual 0.81.0
Rich 13.8.1

Python

Name Value
Version 3.12.6
Implementation CPython
Compiler MSC v.1940 64 bit (AMD64)
Executable C:\Program Files\Python312\python.exe

Operating System

Name Value
System Windows
Release 10
Version 10.0.19045

Terminal

Name Value
Terminal Application Unknown
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=170, height=46
legacy_windows False
min_width 1
max_width 170
is_terminal True
encoding utf-8
max_height 46
justify None
overflow None
no_wrap False
highlight None
markup None
height None
Copy link

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@Textualize Textualize deleted a comment Sep 29, 2024
@Textualize Textualize deleted a comment Sep 29, 2024
@Textualize Textualize deleted a comment Sep 29, 2024
@mitaa mitaa changed the title snap_compare failures using DirectoryTrees and .mount in spite of identical appearance snap_compare failures using DirectoryTrees and .mount in spite of identical appearance Sep 29, 2024
@mitaa
Copy link
Author

mitaa commented Sep 29, 2024

Correction:

    # This works
    # def compose(self):
    #     yield DirectoryTree(ROOT)
    #     yield DirectoryTree(ROOT)

Not entirely.
Using this with 0.81 seems to generate identical svg files, but they are still different from other versions.

Using the code quoted above instead of the on_mount function i get the following

├───0.78.0
│       103ed17be2726a686e7b85e77fa2ab03af85c344aeb012985cda803a8147403d.svg
│
├───0.79.0
│       103ed17be2726a686e7b85e77fa2ab03af85c344aeb012985cda803a8147403d.svg
│
├───0.80.0
│       66ba2bb8610dfac2b1e8ad668ef79dd7b5e2636713fb586f4350e744ba0c2648.svg
│       8cbe0a12044fdeb2c5d763cc079c816d65ab9fda51764a1c498e1f8b9e74414b.svg
│
└───0.81.0
        2769b26e6544461cc1d3d5c6344a64b9353f7e6ef6fcd80417dfb929dfa79089.svg

@willmcgugan
Copy link
Collaborator

Snapshots may fail when you update Textual. This can happen when we change something. More often than not there is no visual change to the output, and you can just accept the changes.

If you always get the same output for a particular version of Textual, then that indicates there is no problem -- assuming the output is what you expected.

If you are getting different output which the same version of Textual, then that most often indicates that the output is being generated differently run-to-run.

From your code I can see you aren't awaiting the mounts or the reload. If you don't await them, they will run at the same time, likely introducing a race condition.

Your sample.py is not using our test framework. Which makes this less predictable. I would suggest testing via a pytest test. If you don't see issues after awaiting the tree operations, then that may be the fix.

@mitaa
Copy link
Author

mitaa commented Sep 29, 2024

You're totally right, awaiting them makes my issue disappear.

Thanks and sorry about that.

@mitaa mitaa closed this as completed Sep 29, 2024
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@willmcgugan
Copy link
Collaborator

Not a problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants